Iterator Discussions for container classes

Source: Internet
Author: User

First-level containers include "sequential containers" and "associative containers", and container adapters do not belong to one-tier containers

Iterators are used to access and control elements in a first-level container

Why is the container adapter not supported? Because the container adapter relies on a specific container implementation.

Forward iterators can also support forward + + and back + +
Lis supports bidirectional iterators, however queue is a container adapter and does not support iterators
The input iterator is used to read the data, and the output iterator is used to write the data, so the output iterator is used for the left value
Stack is a container adapter and cannot use iterators
After the set is saved, it is sorted by the rule (default from small to sort)
int Main () {    list<int> IList;    List<int>::iterator it1 = ilist.begin ();    List<int>::iterator it2 = ilist.end ();     if (It1 > it2)    {        "it1 > It2" << Endl;    }     return 0 ;}

In the following specific analysis.

1, It1,it2 can not compare size, because It1,it2 is a list of iterators, is a bidirectional iterator

2, IT3,IT4 can compare size, because IT3,IT4 is a vector iterator, support random iterators

First of all, why Vector,deque support random iterators, list does not support random iterators?

Vector,deque is a linear structure that can be accessed randomly

List is a linked list is structured and cannot be accessed randomly

Bidirectional iterators cannot compare sizes, random iterators can compare sizes

A bidirectional iterator cannot compare size because the address of an element on a linked list is not contiguous and the size of the comparison is meaningless

A random iterator can compare size, because it refers to the address of an element in a contiguous space that is contiguous, and the size of the comparison is meaningful

Try to use

 for (Itr3=li3.begin (); ITR3! = Li3.end (); itr3++)

Instead of

 for (Itr3=li3.begin (); Itr3 < Li3.end (); itr3++)

Because the former is common to various iterators, the latter is only common to random iterators , and once you consider moving forward from backward, you need to change "<" to ">", for example

 for (Itr3=li3.rbegin (); ITR3 > Li3.rend (); itr3--)

Iterator Discussions for container classes

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.