c++ - Use a regular iterator to iterate backwards, or struggle with reverse_iterator? -
I recently learned the correct way to work with reverse contractors in C ++ (especially when you need to be deleted). (See and.)
To do this you should do this:
typedef std :: vector & lt; Int & gt; Fourth; (IV :: reverse_iterator rit = iv.rbegin (), rend = iv.rend (); rit! = Rend; ++ rit) {// Use 'Write' if a reverse_itator is good enough, for example, * Rit + = 10; // Usage (rit + 1) .base () If you need a regular iterator, such as iv.erase ((rit + 1) .base ()); }
But I thought that it is much better ( do not do it , do not conform to standards, like mowing Doc explains):
(IV :: iterator = iv.end), start = iv.begin (); It--! = Begin;) {// Use 'it' for anything you want * this + = 10; Iv.erase (this); Cons: -
You tell me What has happened to this? - It does not conform to the standards, as the mowing dock explains.
-
-
-
-
-
-
-
-
- / Li>
- Remember also (or explain) works for std :: list:
it = il
- If you erase an element, then You do not have to adjust the iterator
- If you erase, you do not need to recompile the reason for the reverse iterator is that it is not known about it to be repeated.
For example on a back-end:
# Include & lt; String & gt; # Include & lt; Algorithm & gt; Std :: wstring foo ("This is an examination, which contains two letters."); Std :: find (foo.begin (), foo.end (), L 'A'); // An itater indicates that // first for a character. Std :: find (foo.rbegin (), foo.rend (), l'o '). Base () - 1; // gives an Iterter / / previous A stt :: find (foo.end), foo.begin (), L 'A'); //wrong!! (Buffer overrun)
Whatever the use of the result in the intro obvious code.