码迷,mamicode.com
首页 > 编程语言 > 详细

C++ STL

时间:2020-05-09 22:51:19      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:begin   div   nta   into   using   set   second   contain   header   

遍历

set遍历

#include <iostream>
#include<set>
using namespace std;
int main(){
    int numList[6]={1,2,2,3,3,3};
    //1.set add
    set<int> numSet;
    for(int i=0;i<6;i++)
    {
        //2.1insert into set
        numSet.insert(numList[i]);
    }
    //2.travese set
    for(set<int>::iterator it=numSet.begin() ;it!=numSet.end();it++)
    {
        cout<<*it<<" occurs "<<endl;
    }
    //3.set find useage

    int findNum=1;
    if(numSet.find(findNum)!=numSet.end())
    {
        cout<<"find num "<<findNum<<" in set"<<endl;
    }else{
        cout<<"do not find num in set"<<findNum<<endl;
    }
    //set delete useage
    int eraseReturn=numSet.erase(1);
    if(1==eraseReturn)
    {
          cout<<"erase num 1 success"<<endl;
    }else{
        cout<<"erase failed,erase num not in set"<<endl;
    }
    return 0;
}

map遍历

#include <iostream>
#include <map>
using namespace std;

int main(){
    map<int,int> m;
    for (int i = 0; i < 10; i++){
        m[i] = i*i;
    }
    map<int,int>::iterator iter;
    iter = m.begin();
    while(iter != m.end()){
        cout << iter->first << "-" << iter->second << endl;
        iter++;
    }
    for (iter = m.begin();iter != m.end(); iter++){
        cout << iter->first << "-" << iter->second << endl;
    }
    for(auto &it : m){
        cout << it.first << "-" << it.second <<endl;
    }
    return 0;
}

C++ STL

标签:begin   div   nta   into   using   set   second   contain   header   

原文地址:https://www.cnblogs.com/houzm/p/12860348.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!