The iterator of vector and map fails.

Source: Internet
Author: User

1. Vector

# Include <iostream> # include <string> # include <vector> using namespace STD; void vectortest () {vector <int> container; For (INT I = 0; I <10; I ++) {container. push_back (I);} vector <int>: iterator ITER; For (iter = container. begin (); iter! = Container. End (); ITER ++) // cycle 2 {container. Erase (ITER) ;}for (iter = container. Begin (); iter! = Container. end (); ITER ++) {cout <* ITER <Endl ;}} int main (INT argc, const char * argv []) {vectortest (); return 0 ;}

Execution result: 0 1 2 3 5 7 9

When deleting an element in a vector, the following elements are moved forward by one place, so the original iterators are invalid.

After deleting the first number 4 greater than 3: 0 1 2 3 5 6 7 8 9

---> But now the iterator points to 5, ITER ++ points to 6 again, and 5 is missing... As a result, 5, 7, and 9 are not deleted.

Method of modification: In loop 2:

     for (iter = container.begin(); iter != container.end();)    {         if(*iter > 3)               {             container.erase(iter);            continue;         }         iter++;    }

Or:

For (iter = container. Begin (); iter! = Container. end ();) {If (* ITER> 3) {iter = container. erase (ITER); // at this time, the location of the ITER is not changed, but the content to be pointed to becomes a continue behind the original element;} ITER ++ ;}


2. Map failure

Map is an associated container that organizes data with a red/black tree or a balanced binary tree. Although an element is deleted, the entire tree is adjusted to conform to the rules of the red/black tree or binary tree, however, the address of a single node in the memory remains unchanged, and the pointing relationship between nodes is changed.

Http://blog.163.com/?email protected]/blog/static/1322296552010824114547940/

The deletion of red and black trees in http://blog.csdn.net/v_july_v/article/details/6105630 is complicated.


Sequent container: (vector, list, And deque)
The erase iterator not only invalidates the iterator pointing to the deleted element, but also invalidates all iterators after the deleted element. Therefore, the erase (ITER ++) method cannot be used, however, the returned value of erase is the next effective iterator.

So the correct method is ::
For (iter = C. Begin (); iter! = C. End ();)
Iter = C. Erase (ITER );
 
Associated containers: (MAP and set are commonly used)
The erase iterator is invalid only for the iterator whose elements are deleted, but the returned value is void. Therefore, erase (ITER ++) is used to delete the iterator,
So the correct method is ::
For (iter = C. Begin (); iter! = C. End ();)
C. Erase (ITER ++ );

TIPS:
In fact, both methods of list can work normally.


STL containers can delete elements by using the erase (key) method in addition to the iterator.
Size_t rm_num = obj. Erase (key );
Rm_num indicates the number of Members who delete the key. In map, the key is the key value, and in other containers, the key is a value.







 





Zookeeper

The iterator of vector and map fails.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.