stl及函数拾撷

时间:2020-01-09
本文章向大家介绍stl及函数拾撷,主要包括stl及函数拾撷使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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;
}

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