C++标准库---迭代器

180it 2020-10-15 PM 2043℃ 0条

迭代器是一个“可遍历STL容器内全部或部分元素”的对象。一个迭代器用来指出容器中的一个特定位置。

operator*:返回当前位置上的元素值;

operator++:将迭代器前进至下一元素;
operator==和operator!=:判断两个迭代器是否指向同意位置;
operator=:为迭代器赋值。

因为每个容器选择的数据结构不同,所以每一种容器都必须提供自己的迭代器。

begin():返回一个迭代器,指向容器的起点,也就是第一个元素的位置。

end():返回一个迭代器,指向容器的结束点,结束点在最后一个元素之后。

下面看看不同的容器,怎么使用迭代器,以及set,map等容器的见到那使用。

#include<iostream>
#include<list>

using namespace std;

int main()
{
    list<char> str;
    list<char>::const_iterator pos;//只读  iterator 读写
    list<char>::iterator pos_;//读写

    for(char c='a';c<='z';c++)
    {
        str.push_back(c);
    }

    for(pos=str.begin();pos!=str.end();++pos)
    {
        cout<<*pos<<" ";
    }
    cout<<endl;

    for(pos_=str.begin();pos_!=str.end();++pos_)
    {
        if(*pos_<='g')
        {
            *pos_=toupper(*pos_);
        }
        cout<<*pos_<<" ";
    }
    cout<<endl;


    system("pause");
    return 0;
}

从运行结果可可以看出,const_iterator与iterator的区别,只有iterator迭代器可以读写数据。

//set 迭代器
#include<iostream>
#include<set>
 
using namespace std;
 
int main()
{
    typedef set<int> IntSet;//set 不允许重复 默认排序由小到大
    //typedef multiset<int> IntSet;//multiset 允许重复 默认排序由小到大
 
    IntSet coll;
 
    coll.insert(3);
    coll.insert(1);
    coll.insert(5);
    coll.insert(4);
    coll.insert(1);
    coll.insert(6);
    coll.insert(2);
    coll.insert(-5);
 
    IntSet::const_iterator pos;//只读
    for(pos=coll.begin();pos!=coll.end();++pos)
    {
        cout<<*pos<<" ";
    }
    cout<<endl;
 
    system("pause");
    return 0;
}

运行结果:
-5 1 2 3 4 5 6

//multimap 迭代器
#include<iostream>
#include<map>
#include<string>
 
using namespace std;
 
int main()
{
    typedef multimap<int,string> IntStringMMap;//第一个元素为键值,第二个元素为实值
 
    IntStringMMap coll;
 
    coll.insert(make_pair(5,"tagged"));
    coll.insert(make_pair(2,"a"));
    coll.insert(make_pair(1,"this"));//这里的1先被加入
    coll.insert(make_pair(4,"of"));
    coll.insert(make_pair(6,"strings"));
    coll.insert(make_pair(1,"is"));//这里的1加到第一个1后面
    coll.insert(make_pair(3,"multimap"));
 
    IntStringMMap::const_iterator pos;//只读
    for(pos=coll.begin();pos!=coll.end();++pos)
    {
        cout<<pos->second<<" ";
    }
    cout<<endl;
 
    system("pause");
    return 0;
}

运行结果:
this is a multimap of tagged strings

//multimap 迭代器
#include<iostream>
#include<map>
#include<string>
 
using namespace std;
 
int main()
{
    typedef map<string,float> StringFloatMap;
 
    StringFloatMap coll;
    StringFloatMap::iterator pos;
 
    coll["VAT"]=0.15;
    coll["Pi"]=3.1415;
    coll["an arbitrary number"]=4983.233;
    coll["Null"]=0;
 
    for(pos=coll.begin();pos!=coll.end();++pos)
    {
        cout<<"Key: \""<<pos->first<<"\" "<<"value: "<<pos->second<<endl;
    }
 
    system("pause");
    return 0;
}

set:

set的内部元素依据其值自动排序,每个元素值只能出现一次,不允许重复。

multiset:

multiset和set相同,只不过它允许重复元素,也就是说multiset可包括多个数值相同的元素。

map:

map的元素都是“实值/键值”所形成的一个队组,每个元素有一个键,是排序准则的基础。每一个键只能出现一次,不允许重复。(第一个元素为键值,第二个元素为实值)

multimap:

multimap和map相同,但允许重复元素,也就是说multimap可包含多个键值相同的元素。

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

C++标准库---迭代器