In C++,
std::vector
is a dynamic array that provides flexible memory management and rich member functions.insert
The function isstd::vector
A very useful member function provided for inserting an element or another range of elements at a specified location.
std::vector::insert
Usage
1. Insert a single element
iterator insert(const_iterator pos, const T& value);
pos
: Iterator for inserting position (pointing to the previous element of the position to be inserted).
value
: The element to be inserted.
2. Insert multiple identical elements
void insert(const_iterator pos, size_type count, const T& value);
pos
: Iterator for insertion position.
count
: The number of elements to be inserted.
value
: The element to be inserted.
3. Insert an element in a range
template <class InputIterator> void insert(const_iterator pos, InputIterator first, InputIterator last);
pos
: Iterator for insertion position.
first
: The starting iterator of the range.
last
: End iterator for range.
4. Insert elements in the initialization list (C++11 and above)
void insert(const_iterator pos, initializer_list<T> ilist);
pos
: Iterator for insertion position.
ilist
: Initialize the list.
Sample code
Example 1: Insert a single element
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; (() + 2, 99); // Insert 99 at index 2 for (int num : vec) { cout << num << " "; } cout << endl; return 0; }
Output:
1 2 99 3 4 5
Example 2: Insert multiple identical elements
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; (() + 2, 3, 99); // Insert 3 99s at index 2 for (int num : vec) { cout << num << " "; } cout << endl; return 0; }
Output:
1 2 99 99 99 3 4 5
Example 3: Insert an element in a range
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec1 = {1, 2, 3, 4, 5}; vector<int> vec2 = {99, 100, 101}; (() + 2, (), ()); // Insert all elements of vec2 at index 2 for (int num : vec1) { cout << num << " "; } cout << endl; return 0; }
Output:
1 2 99 100 101 3 4 5
Example 4: Insert an element in the initialization list
#include <iostream> #include <vector> using namespace std; int main() { vector<int> vec = {1, 2, 3, 4, 5}; (() + 2, {99, 100, 101}); // Insert element in the initialization list at index 2 for (int num : vec) { cout << num << " "; } cout << endl; return 0; }
Output:
1 2 99 100 101 3 4 5
Things to note
Iterator failed:
The insertion operation invalidates all iterators after the insertion point.If you need to continue using the iterator after insertion, it is recommended to re-get it.
For example:
auto it = () + 2; (it, 99); it = () + 2; // Retrieve the iterator
performance:
The time complexity of the insertion operation is O(n), where n is the number of elements after the insertion point. This is because the insertion operation may require moving all elements after the insertion point.
Memory allocation:
If the insertion operation causes
vector
Insufficient capacity,vector
Memory will be reallocated automatically, which may cause all iterators to fail.
Summarize
std::vector::insert
It is a very flexible function that can be used to insert a single element, multiple identical elements, a range of elements, or an initialization list at a specified location. By reasonable useinsert
Functions that can be operated easilyvector
content.
This is the article about the usage of insert function of C++ vector. For more information about insert function of C++ vector, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!