Essential C++ std::vector Member Functions and Algorithms
Basic Element Manipulation
push_back()
Adds an element to the end of the vector.
std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
pop_back()
Removes the last element from the vector.
numbers.pop_back();
insert()
Inserts elements at a specified position.
std::vector<int> data {5, 10, 15};
data.insert(data.begin() + 1, 25); // Result: {5, 25, 10, 15}
erase()
Removes elements at a specified position.
std::vector<int> data {5, 10, 15};
data.erase(data.begin() + 1); // Result: {5, 15}
Capacity and Access
size()
Returns the number of elements in the vector.
empty()
Returns true if the vector contains no elements.
clear()
Removes all elemenst from the vector.
front()
Returns a reference to the first element.
back()
Returns a reference to the last element.
std::vector<double> values {3.14, 2.71, 1.41};
double first = values.front(); // 3.14
double last = values.back(); // 1.41
asign()
Replaces the contents of the vector with new elements.
std::vector<char> letters {'a', 'b', 'c'};
letters.assign(4, 'x'); // Result: {'x', 'x', 'x', 'x'}
Finding Min and Max Elements
min_element()
Locates the smallest element in a range.
std::vector<int> scores {45, 90, 23, 67};
auto minPos = std::min_element(scores.begin(), scores.end());
if (minPos != scores.end()) {
int minimum = *minPos;
}
max_element()
Locates the largest element in a range.
auto maxPos = std::max_element(scores.begin(), scores.end());
int maxIndex = std::distance(scores.begin(), maxPos);
int maximum = *maxPos;
Sorting with stable_sort()
Preserevs the relative order of equivalent elements.
struct Student {
int id;
int grade;
};
std::vector<Student> roster;
roster.push_back({3, 85});
roster.push_back({1, 92});
roster.push_back({2, 85});
bool compareByGrade(const Student& a, const Student& b) {
if (a.grade == b.grade) {
return a.id < b.id;
}
return a.grade < b.grade;
}
std::stable_sort(roster.begin(), roster.end(), compareByGrade);
Conditional Search with find_if()
The function signature:
template<class InputIt, class UnaryPredicate>
InputIt find_if(InputIt first, InputIt last, UnaryPredicate pred);
Simple Predicate Example
bool isOdd(int value) {
return value % 2 != 0;
}
std::vector<int> nums {4, 7, 9, 12};
auto result = std::find_if(nums.begin(), nums.end(), isOdd);
if (result != nums.end()) {
std::cout << "First odd number: " << *result << std::endl;
}
Lambda with Captured Variables
struct Person {
std::string identifier;
std::string name;
std::string department;
int yearsExp;
};
std::vector<Person> employees;
std::string searchId;
std::cin >> searchId;
auto matchId = [&searchId](const Person& p) {
return p.identifier == searchId;
};
auto found = std::find_if(employees.begin(), employees.end(), matchId);
Helper Function as Predicate
bool matchesIdentifier(const Person& p, const std::string& target) {
return p.identifier == target;
}
std::string targetId;
std::cin >> targetId;
auto iter = std::find_if(employees.begin(), employees.end(),
[&targetId](const Person& p) {
return matchesIdentifier(p, targetId);
});
Iterating Through Vectors
Range-Based For Loop
void displayValues(const std::vector<int>& collection) {
for (int element : collection) {
std::cout << element << " ";
}
std::cout << std::endl;
}