C + + in the const and iterator (iterator) use of the detailed

Source: Internet
Author: User
Tags include modify

An iterator (iterator) is a pointer type, and also points to the Const pointer itself (address) and the value indicated by the const pointer, in two cases;

But the writing and const built-in pointers are different;

char * const is equivalent to const container<>::iterator; You can modify the value that the pointer refers to, but you cannot modify the pointer's address;

const char * equivalent to container<>::const_iterator; You can modify the pointer address, but you cannot modify the value indicated by the pointer;

Note The code example, two const and iterators;

Code:

* * * * effectivecpp.cpp * *  Created on:2013.11.13 *      author:caroline
      
* */*eclipse CDT; GCC 4.7.1*/
      
#include <iostream>  
#include <vector>  
      
int main (void) {  
      
    std::vector< Int> IV = {1, 2, 3, 4, 5};  
    Const AUTO IT = Iv.begin (); The const iterator is equivalent to t* const, and the pointer itself (address) is fixed;  
    Std::cout << "*it =" << *it << Std::endl;  
    ++it; Error  
    *it = ten;  
    Std::cout << "*it =" << *it << Std::endl;  
      
     IV = {1, 2, 3, 4, 5};  
     Auto CIT = Iv.cbegin (); Cbegin, equivalent to Const_iterator, the finger of the pointer is fixed;  
     Std::vector<int>::const_iterator cit = Iv.begin ();  
     Std::cout << "*cit =" << *cit << Std::endl;  
     ++cit;  
     *cit = 10; Error  
     std::cout << "*cit =" << *cit << Std::endl;  
      
    return 0;  
      
}

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.