C++:各个容器中迭代器的用法

迭代器(Iterator)详解见博客:C++:迭代器Iterator详解

以下内容转载自https://blog.csdn.net/qq_23100787/article/details/51388163
下面列举了些例子说明各个容器的用法:
1、Vector

#include 
#include 

int main()
{
    std::vector<char> charVector;
    int x;

    for(x=0; x<10; ++x)
        charVector.push_back(65 + x);

    int size = charVector.size();

    for(x=0; x<size; ++x)
    {
        std::vector<char>::iterator start = charVector.begin();
        charVector.erase(start);
        std::vector<char>::iterator iter;
        
        for(iter = charVector.begin();iter != charVector.end(); iter++)
        {
            std::cout << *iter;
        }
        std::cout << std::endl;
    }
    return 0;
}

2、deque

#include 
#include 

int main()
{
    std::deque<char>  charDeque;
    int x;

    for(x=0; x<10; ++x)
         charDeque.push_back(65 + x);

    int size =  charDeque.size();

    for(x=0; x<size; ++x)
    {
        std::deque<char>::iterator start =  charDeque.begin();
         charDeque.erase(start);
        std::deque<char>::iterator iter;
        
        for(iter =  charDeque.begin();iter !=  charDeque.end(); iter++)
        {
            std::cout << *iter;
        }
        std::cout << std::endl;
    }
    return 0;
}

3、list

#include 
#include 

int main()
{
    std::list<char>  charList;
    int x;

    for(x=0; x<10; ++x)
         charList.push_back(65 + x);

    int size =  charList.size();

    for(x=0; x<size; ++x)
    {
        std::list<char>::iterator start =  charList.begin();
         charList.erase(start);
        std::list<char>::iterator iter;
        
        for(iter =  charList.begin();iter !=  charList.end(); iter++)
        {
            std::cout << *iter;
        }
        std::cout << std::endl;
    }
    return 0;
}

set、multimap等容器中迭代器的应用详见原博客

你可能感兴趣的:(C++)