Methods of vector using iterators

Location
  1. Courses

    /

  2. Complete C++ Course

    /

  3. The C++ Standard Library

    /

  4. Containers

    /

  5. Methods of vector using iterators

Inserting elements

Inserting one element

In order to insert elements inside a vector, we must use iterators. The method overload to insert one element is std::vector<T>::iterator std::vector<T>::insert(std::vector<T>::const_iterator pos, const T & val). An element with the value of the second argument is inserted before the element pointed by the iterator given in argument. An iterator pointing to the newly inserted element is returned.

Note that before C++11, the iterator in argument is of type iterator rather than const_iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream> #include <vector> int main() { std::vector<float> vec; vec.resize(4); for(unsigned c=0; c< vec.size(); c++) vec[c] = c; std::vector<float>::iterator it = vec.begin() + 1; // Inserts an element after the first one. vec.insert(it, 9); it = vec.end() - 1; // Inserts an element before the last element and makes the iterator 'it' point to it. it = vec.insert(it, 9); // Changes the value of the newly inserted element. *it = 7; for(unsigned c=0; c< vec.size(); c++) std::cout << vec[c] << std::endl; return 0; }

Inserting the same element many times

Using the method overload std::vector<T>::iterator std::vector<T>::insert(std::vector<T>::const_iterator pos, size_type n, const T & val) is used to insert, before the element pointed by the iterator given in first argument, an element with the value given in last argument, the number of time specified by the second argument. An iterator to the first element inserted is returned.

Note that before C++11, the return type of that method is void and the iterator in argument is of type iterator rather than const_iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> #include <vector> int main() { std::vector<float> vec; vec.resize(4); for(unsigned c=0; c< vec.size(); c++) vec[c] = c+1; std::vector<float>::iterator it = vec.begin() + 2; // Inserts 4 zeros at the middle of the vector. vec.insert(it, 4, 0); for(unsigned c=0; c< vec.size(); c++) std::cout << vec[c] << std::endl; return 0; }

Inserting a range of elements

Using the method overload template<class IteratorT> std::vector<T>::iterator std::vector<T>::insert(std::vector<T>::const_iterator pos, IteratorT first, IteratorT last), we can insert, before the element pointed by the first iterator, the elements in the range starting with the element pointed by the second argument (included), until the one pointed by the last argument (not included). An iterator to the first element inserted is returned.

Note that before C++11, the return type of that method is void and the first iterator in argument is of type iterator rather than const_iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream> #include <vector> int main() { std::vector<int> vec(2, 1); std::vector<int> vec2(5, 0); // Inserts the elements of 'vec2' at the middle of the vector 'vec'. vec.insert(vec.begin()+1, vec2.begin(), vec2.end()); for(unsigned c = 0; c < vec.size(); c++) std::cout << vec[c] << std::endl; return 0; }

Because the two last arguments of the method are of template type, we can do the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream> #include <vector> int main() { char array[] ={'_', '_', '_', '_'}; std::vector<char> vec(2, '%'); // Inserts the elements of the static array at the middle of the vector. vec.insert(vec.begin()+1, array, array+4); for(unsigned c = 0; c < vec.size(); c++) std::cout << vec[c]; std::cout << std::endl; return 0; }

Erasing elements

Erasing an element

Using the method overload std::vector<T>::iterator erase(std::vector<T>::const_iterator pos), we can erase a single element from a vector. The element pointed by the iterator in argument is removed. An iterator to the element following the erased element is returned (std::vector<T>::end() if it was the last).

Note that before C++11, the iterator in argument is of type iterator rather than const_iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream> #include <vector> int main() { std::vector<int> vec(4); for(unsigned c = 0; c < vec.size(); c++) vec[c] = c+1; // Erases the two elements in the middle of the vector. std::vector<int>::iterator it = vec.begin() + 1; it = vec.erase(it); vec.erase(it); for(unsigned c = 0; c < vec.size(); c++) std::cout << vec[c] << std::endl; return 0; }

Using the method overload std::vector<T>::iterator erase(std::vector<T>::const_iterator first, std::vector<T>::const_iterator last), we can remove a range of elements from a vector, starting from the element pointed by the first argument (included) until the element pointed by the second one (Excluded).

An iterator to the element following the last element erased is returned (std::vector<T>::end() if it was the last).

Note that before C++11, the iterators in argument are of type iterator rather than const_iterator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream> #include <vector> int main() { std::vector<int> vec(9); for(unsigned c = 0; c < vec.size(); c++) { vec[c] = c+1; std::cout << vec[c]; } std::cout << std::endl; // Erases 5 elements from the middle of the vector. vec.erase(vec.begin()+2, vec.end()-2); for(unsigned c = 0; c < vec.size(); c++) std::cout << vec[c]; std::cout << std::endl; return 0; }