YunYan

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

STL的迭代器听起来怪吓人的,其实并不是什么高深的东西,说白了就是定义了一个指向STL的指针。、

对于没个STIL都可以定义

set,,vector ,,map,,,string

定义: set<int >::iterator it;(以set与int为例)

可以用于遍历set中的元素  比如

for(it=s.begin();it!=s.end();it++){
    cout<<*it<<endl;
}

这样就可以输出集合中的元素了

对于map

for(it=s.begin();it!=s.end();it++){
    cout<<it->first<<endl;//对应key
    cout<<it->second<<endl;//对应value
}

string也可以用来遍历

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string a;
    cin>>a;
    string ::iterator it;
    for(it=a.begin();it!=a.end();it++){
        cout<<*it<<endl;
    }
    return 0;
}
it会指向string的每个字符 并且对每个字符进行输出;

vector也可以用迭代器;由于vector比较灵活 所以通常很少用

STL中erase的用法

https://www.cnblogs.com/wangkundentisy/p/9023977.html 

posted on 2019-07-21 20:34  Target--fly  阅读(284)  评论(0)    收藏  举报