Summary
Iterators are an important part of STL, Prev and distance are the basic methods. Distance method is very simple, do not repeat here, is mainly the Prev method and its related methods--advance method for a brief introduction and use of the instructions, and at the end of the text attached code examples.
"Advance Method"
Advance iterator
Advances the iterator it by n element positions.
If It is a random-access iterator, the function uses just once operator+ or operator-. Otherwise, the function uses repeatedly the increase or decrease operator (operator++ or operator--) until n elements has been advanced.
Translated
The advance iterator is to move the iterator it, moving n bits. If it is a random-access iterator, then the function does a 1-time operator calculation operation, otherwise the function will perform an n-iteration calculation on the iterator.
code example
Advance Example#include <iostream> //Std::cout#include <iterator> //Std::advance#include <list> //Std::listint main () { std::list<int> mylist; for (int i=0; i<10; i++) Mylist.push_back (i*10); Std::list<int>::iterator it = Mylist.begin (); Std::advance (it,5); Std::cout << "The sixth element in MyList is:" << *it << ' \ n ';//output 0;}
parsing
Note that moving 5 times, the output is a 6th element instead of the 5th element.
"Prev Method"
Get iterator to previous elementReturns An iterator pointing to the element, that would is pointing to if advanced it
-n
positions.
If it is a random-access iterator, the function uses just once
operator+
or
operator-
. Otherwise, the function uses repeatedly the increase or decrease operator (
operator++
or
operator--
) on the copied iterator until elements has been advanced.
translation
In the case of an on-board access iterator, only one operator operation (+ or-) is performed, otherwise the n continuous decrement or increment operation is performed.
code example
<pre name= "code" class= "CPP" > #include <iostream> //Std::cout#include <iterator> //std:: Advance#include <list> //Std::list#include <algorithm>int main () { std::list<int> mylist; for (int i=0; i<10; i++) Mylist.push_back (i*10);//std::cout<<*upper_bound (Mylist.begin (), Mylist.end () , <<std::endl;//throws an exception Std::cout<<*lower_bound (Mylist.begin (), Mylist.end (), 0) <<STD:: endl;//Output 0 std::cout<<*prev (upper_bound (Mylist.begin (), Mylist.end (), +) <<std::endl;//output 90
Std::cout<<*prev (++upper_bound (Mylist.begin (), Mylist.end (), ()) <<std::endl;//throws an exception//std::cout< <*prev (Lower_bound (Mylist.begin (), Mylist.end (), 0)) <<std::endl;//throws an exception return 0;}
Analytical
Prev in the VC6.0 can not be achieved, in the VS2010. The actual implementation of the operation is to decrement the iterator by one unit length, not see the so-called increment operation or according to whether as a random iterator when the operation of one or n times!
"Re-Reel"
See: Leetcode Search for a Range (find)
Detailed Address: http://blog.csdn.net/u013630349/article/details/47099915
Class Solution {public: vector<int> searchrange (vector<int>& nums, int target) { const int l = di Stance (Nums.begin (), Lower_bound (Nums.begin (), Nums.end (), target)); const int u = Distance (Nums.begin (),--upper_bound (Nums.begin (), Nums.end (), target)); if (nums[l]! = target)//not found return vector<int> {-1,-1}; else return vector<int> {l, u};} };
Analytical:
SOURCE verification AC, just on the original basis will be "prev (... ) "instead"-(... )”。
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Analysis of advance and Prev methods of C + + STL iterator method