A brief introduction to some standard template containers for C + + (2)

Source: Internet
Author: User

To continue with the previous article:


1. For const_iterator, we cannot change the value of the container it points to, but we can change the value of the const_iterator itself.

Vector<int> VEC1 (10,3); Vector<int>::const_ieterator myiter = Vec1.begin (); *myiter = 2; Error Const_iterator is a constant after the dereference, ++myiter; Ok


2. You need to distinguish between the Const_iterator object and the Const iterator object.

Const_iterator means that we cannot change the value of a container element by using the Const_iterator dereference.

The const iterator object refers to: The iterator object itself cannot be changed, but the value of the object pointed to by the iterator can be modified by dereference.

Vector<int> Vec1;vec1.push_back (1); Vec1.push_back (3); Vec1.push_back (3); Const Vector<int>::iterator It1 = Vec1.begin ();//const's iterator object must be initialized. ++it1;  Error,const object cannot be modified *it1 = 100; OK, it1 is a constant but can be modified by the value he points to.



3. Arithmetic operation of the iterator:

(1) The Iterator object can be added or subtracted by N (n must be an integer value), where n is the Size_type or difference_type type. adding n or subtracting n must ensure that the iterator after the operation is within the container return, between the Begin () and end () functions.


(2) Iter1-iter2, the result of this operation is the distance between the two iterator objects. The result of the operation may be a positive value, possibly a negative value, or 0. The type of the result of the operation is the signed type of the Difference_type type. Both iterator objects must point to the same container or to the next element after the end of the container (the return value of the end () function).


(3) Any action that alters the number of container elements invalidates the container's iterator. Therefore, when we use the container, we use iterators to determine whether the container is over. It is not recommended to use an integer value to hold the length of the container to determine whether the container is traversed.

This article is from "Home and Everything" blog, please make sure to keep this source http://louis1126.blog.51cto.com/2971430/1676328

A brief introduction to some standard template containers for C + + (2)

Related Article

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.