STL set 与 map 实例

来源:互联网 发布:越南手机网络制式 编辑:程序博客网 时间:2024/05/22 12:52

set:

////  081_STL set.cpp//  changlle////  Created by user on 12/30/15.//  Copyright (c) 2015 user. All rights reserved.//#include <iostream>#include <set>using namespace std;int main () {        set<int> s;        s.insert(1);    s.insert(3);    s.insert(5);        set<int>::iterator ite;        ite=s.find(1);    if( ite==s.end()) cout<<"not found"<<endl;    else cout<<"found"<<endl;        ite=s.find(2);    if (ite==s.end()) cout<<"not found"<<endl;    else cout<<"found"<<endl;        s.erase(3);        ite=s.find(3);    if (ite==s.end()) cout<<"not found"<<endl;    else cout<<"found"<<endl;            for ( ite=s.begin();ite!=s.end();++ite)        cout<<*ite<<endl;            return 0;}

map:

////  082_STL map.cpp//  changlle////  Created by user on 12/30/15.//  Copyright (c) 2015 user. All rights reserved.//#include <iostream>#include <map>#include <string>using namespace std;int main() {        map<int, const char*> m;        m.insert(make_pair(1,"one"));    m.insert(make_pair(10,"ten"));    m[100]="hundred";            map<int, const char*>::iterator ite;    ite=m.find(1);        cout<<ite->second<<endl;        ite =m.find(2);    if (ite==m.end()) cout<<"not found"<<endl;    else cout<<ite->second<<endl;        cout<<m[10]<<endl;        m.erase(10);        for (ite=m.begin(); ite!=m.end();ite++)        cout<<ite->first<<"  "<<ite->second<<endl;                    return 0;}


0 0
原创粉丝点击