首页 > 其他 > 详细

stl及函数拾撷

时间:2020-01-09 18:51:59      阅读:63      评论:0      收藏:0      [点我收藏+]

tools-->compiler options-->"-std=c++11"

set,iterator

//reference: https://blog.csdn.net/sunshinewave/article/details/8068326
#include <iostream>
#include <algorithm>
#include <set> 
using namespace std;
 
 int main()
 {
    set<int> s;   
    set<int>::iterator first;
    set<int>::iterator second;
    
    //插入 
    for(int i = 1 ; i <= 10 ; ++i)
    {
        s.insert(i);
    }
    //第一种删除
    s.erase(s.begin());
    
    
    
    //第二种删除
    first = s.begin();
    second = s.begin();
    second++;
    s.erase(first,second);
    
    
    //第三种删除
    s.erase(8);
    
    
    //判断是否在集合中
     if (s.count(11)!=1) cout<<'N';
     if (s.find(10)!=s.end()) cout<<'Y'; 
     cout<<endl;     
    
 
    //第二种插入 
    int a[] = {1,2};//cout<<a[3]----> "3960616" 
    s.insert(a,a+2);
    
    
    //最后的数据刚好是从1到10,中间只少了个8 
    
    //反向输出 
    set<int>::reverse_iterator riter;
    for(riter = s.rbegin() ; riter != s.rend() ; ++riter)
    {
        cout<<*riter<<" ";
    } 
    cout<<endl;  
     
     //正向输出 
    for(auto iter = s.begin();iter != s.end(); ++iter)
    {
        cout<<*iter<<" ";
    }   
    cout<<endl;     
    
    int b[8]={4,10,11,12,69,70,96,100};
    int low=lower_bound(b,b+8,11)-b;//>=
    int up=upper_bound(b,b+8,11)-b;//>
    cout<<low<<up<<endl;
    
    
    return 0;
}

map

//reference: https://blog.csdn.net/wu_lai_314/article/details/8440550
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#include <map> 
using namespace std;
 
 int main()
 {
    map<string,int> s;
    map<string,int>::iterator iter;      
    //
    s["one"]=1;
    
    pair<string,int> couple("two",3);
    couple.second=2;
    s.insert(couple);
    
    for(iter = s.begin(); iter != s.end(); iter++)
        cout<<iter->first<<" "<<iter->second<<endl;
    cout<<s["one"]<<endl;
    
    iter = s.find("two");
    cout<<iter->first<<" "<<iter->second<<endl;
    
    iter = s.find("three");
    if (iter==s.end()) cout<<"404"<<endl;
    return 0;
}

stl及函数拾撷

原文:https://www.cnblogs.com/reshuffle/p/12172478.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!