iterator code>), which come in several types. Iterator - data structure that « indicates» on some container element, and (for some containers) can navigate to the previous/next element. The vector uses the most powerful - random-access iterator(random-access Iterator). A random access iterator can access an arbitrary element of the vector in addition to sequential movement. The Benefits of Iterators 1) When deleting elements and iterating over elements using indices ([]), we need to keep track of the number of remaining elements all the time so as not to go beyond the vector, and using an iterator, you can use end()< /code> indicating the end of the vector. 2) Using an iterator, you can easily dynamically remove and insert elements in a vector. Iterator declaration 1) Declaring an iterator for an integer vector and pointing it to the first element in the vector. vector <int> myvector = { 1, 2, 3, 4, 5 }; vector <int>::iterator it = myvector.begin(); 2) Declaring an iterator for an integer vector and pointing it to the element after the last in the vector. vector <int> myvector = { 1, 2, 3, 4, 5 }; vector <int>::iterator it = myvector.end(); // points to the element after the last one, vector <int>::iterator it1 = myvector.end() - 1 ; // pointing to the last element. Getting and displaying a value Getting and displaying the element pointed to by the iterator. cout << *it; Move iterator position Move the iterator position 3 positions forward. advance(it, 3); Creating a new iterator from an existing one Create a new iterator based on an existing one, advancing 3 positions. auto it1 = next(it, 3); Displaying vector values using an iterator vector<int>::iterator it; for (it = myvector.begin(); it != myvector.end(); ++it) { cout<<*it<<" "; } Vector traversal To traverse the vector from the last element to the first, a reverse iterator reverse_iterator is used, it is obtained by: 1) rbegin() - returns a reverse iterator pointing to the last element of the vector, the application of the ++ operation leads to the transition to the previous element; 2) rend() - returns a reverse iterator pointing to the previous element of the vector, the application of the ++ operation leads to the transition to the next. vector<int>::reverse_iterator it = myvector.rbegin(); // points to the last element vector<int>::reverse_iterator it = myvector.rend(); // points to an element, // which comes before the first one,
random-access Iterator
[]
end()< /code> indicating the end of the vector. 2) Using an iterator, you can easily dynamically remove and insert elements in a vector. Iterator declaration 1) Declaring an iterator for an integer vector and pointing it to the first element in the vector. vector <int> myvector = { 1, 2, 3, 4, 5 }; vector <int>::iterator it = myvector.begin(); 2) Declaring an iterator for an integer vector and pointing it to the element after the last in the vector. vector <int> myvector = { 1, 2, 3, 4, 5 }; vector <int>::iterator it = myvector.end(); // points to the element after the last one, vector <int>::iterator it1 = myvector.end() - 1 ; // pointing to the last element. Getting and displaying a value Getting and displaying the element pointed to by the iterator. cout << *it; Move iterator position Move the iterator position 3 positions forward. advance(it, 3); Creating a new iterator from an existing one Create a new iterator based on an existing one, advancing 3 positions. auto it1 = next(it, 3); Displaying vector values using an iterator vector<int>::iterator it; for (it = myvector.begin(); it != myvector.end(); ++it) { cout<<*it<<" "; } Vector traversal To traverse the vector from the last element to the first, a reverse iterator reverse_iterator is used, it is obtained by: 1) rbegin() - returns a reverse iterator pointing to the last element of the vector, the application of the ++ operation leads to the transition to the previous element; 2) rend() - returns a reverse iterator pointing to the previous element of the vector, the application of the ++ operation leads to the transition to the next. vector<int>::reverse_iterator it = myvector.rbegin(); // points to the last element vector<int>::reverse_iterator it = myvector.rend(); // points to an element, // which comes before the first one,
advance(it, 3);
reverse_iterator
rbegin()
++
rend()
N
1000 ms 256 Mb Rules for program design and list of errors in automatic problem checking