C++ 标准模板库(STL)——迭代器(iterators)的用法及理解

2021-07-24 00:55

阅读:598

标签:模板库   mes   include   start   turn   begin   pre   理解   ace   

C++ STL中迭代器(iterators)用于遍历对象集合的元素。这些集合可能是容器,也可能是容器的子集。

举例:(1)set的遍历:

#include
#includeset>        
using namespace std;

int main()
{
    setint> int_set;
    for (int i = 0; i 5; i++)
    {
        int_set.insert(i);            
    }
    for (auto start = int_set.begin(); start != int_set.end(); start++)
    {                       
        cout  endl;
    }return 0;
}

(2)map的遍历:

#include
#includeusing namespace std;

int main()
{
    mapint, char> my_map;                                 
    my_map[0] = a;                                        
    my_map[1] = b;
    my_map.insert(mapint, char>::value_type(2, c));       
    for (auto it = my_map.begin(); it != my_map.end(); it++) //map.begin()、map.end():返回map的首、尾迭代器
    {
        cout "key: " first " value: " second  endl;
    }return 0;
}

C++ 标准模板库(STL)——迭代器(iterators)的用法及理解

标签:模板库   mes   include   start   turn   begin   pre   理解   ace   

原文地址:https://www.cnblogs.com/JCpeng/p/15001728.html


评论


亲,登录后才可以留言!