• STL 常见操作


    stl的操作不是很熟练, 记录一下

    map

    map默认是按照key从大到小排序的

    从小到大排序,加个greater<int>

    erase

        it=mymap.find('b');
        mymap.erase (it);                   // erasing by iterator
    
        mymap.erase ('c');                  // erasing by key
    
        it=mymap.find ('e');
        mymap.erase ( it, mymap.end() );    // erasing by range

    vector:

    lower_bound and upper_bound

    copy

        vector<int> v1={1,2,3};
    
        vector<int> v2(v1);
    
        vector<int> v3=v1;
    
        vector<int> v4;
        v4.assign(v1.begin(),v1.end());
        
        vector<int> v5;
        copy(v1.begin(), v1.end(), back_inserter(v5)); 

    resize(int n, int val=0)

    如果n比原size大,那么会默认填充0,如果比原来小,那么会删除末尾的元素

    insert

        vector<int> v1={1,2,3};
        vector<int> v2={11,22,33};
        auto it=v1.end();
        v1.insert(it,2);    //在末尾插入1个2,插入后后面的元素会往后移一位
    
        it=v1.end();    //插入后,iterator会失效
        v1.insert(it,3,999);    // 在末尾插入3个999
    
        it=v1.end();
        v1.insert(it,v2.begin(),v2.end());  //插入某个vector的一段值
    
        int myarray [] = { 501,502,503 };   //插入某个数组的一段值
        v1.insert (v1.end(), myarray, myarray+3);
    
        for(auto i:v1){
            cout<<i<<endl;
        }

    sort:

    sort(v1.begin(),v1.end(),[](int a, int b){return a>b;});

    erase:

        v1.erase (v1.begin()+5);
        //erase the first 3 elements:
        v1.erase (v1.begin(),v1.begin()+3);

    去重:

    sort(vc.begin(),vc.end());

    num.erase(unique(vc.begin(),vc.end()),vc.end());

     

    priority_queue:

    从小到大排:  priority_queue<int, vector<int>, greater<int> > q;

    从大到小排:  priority_queue<int> q;

    string

    string[i][j]可用

    srt(str,str+n),排序

    其他

    next(it,n)

        vector<int> v1={1,2,3,4,5,6};
        auto it=v1.begin();
        cout<<*next(it,3)<<endl;

    next_permutation  & prev_permutation

        vector<int> vc({1,2,3});
        sort(vc.begin(),vc.end());
        for(auto i:vc){
            cout<<i<<" ";
        }
        cout<<endl;
        while(next_permutation(vc.begin(),vc.end())){
            for(auto i:vc){
                cout<<i<<" ";
            }
            cout<<endl;
        }

    Split String

    vector<string> getLines(string& input) {
        istringstream iss(input);
        string curr;
        vector<string> lines;
        while (getline(iss, curr, '
    ')) lines.push_back(curr);
        return lines;
    }

    HEAP

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include<map>
    #include<stack>
    #include<unordered_map>
    #include<ctime>
    #include<sstream>
    using namespace std;
    #define LL __int64
    #define INF 0x3f3f3f3f
    const int MAXN=20;
    #define mod 1000000007
    const double eps=1e-5;
    int dir[4][2]={-1,0,0,1,1,0,0,-1};
    using namespace std;
    class MaxHeap{
    public:
        vector<int> vals;
        MaxHeap(){
    
        }
    
        void addE(int val){
            vals.push_back(val);
            HeapifyUp(vals.size()-1);
        }
    
        void deleteE(int index){
            swap(vals[vals.size()-1],vals[index]);
            vals.pop_back();
            HeapifyDown(index);
        }
    
        void HeapifyUp(int i){
            while(i!=0){
                int p=(i-1)/2;
                if(vals[i]<=vals[p])   return;
                swap(vals[i],vals[p]);
                i=p;
            }
        }
    
        void HeapifyDown(int i){
            while(true){
                int c1=i*2+1;
                int c2=i*2+2;
    
                if(c1>=vals.size()) return; //no child
    
                int c=c2<vals.size()&&vals[c1]>vals[c2]?c1:c2;
    
                if(vals[c]<=vals[i])    return;
    
                swap(vals[i],vals[c]);
                i=c;
            }
        }
    
        int Max(){
            return vals.empty()?0:vals[0];
        }
        
    };
    int main(){
        MaxHeap m;
        m.addE(1);
        m.addE(2);
        m.deleteE(0);
        cout<<m.Max()<<endl;
    }
    View Code
  • 相关阅读:
    Linux(Ubuntu)总结(三)
    Linux(Ubuntu):搭建Docker
    JavaScript数字格式化方法:千分位,百分数
    解决mysqld --install:Install/Remove of the Service Denied!
    【转】Oracle基础结构认知——oracle内存结构 礼记八目 2017-12-15 20:31:27
    【转】Oracle基础结构认知—初识oracle 礼记八目 2017-12-12 21:19:30
    Linux 常用命令
    oracle AWR详解
    VMware 虚拟化技术 创建虚拟机
    MySQL数据库各个版本的区别
  • 原文地址:https://www.cnblogs.com/cnblogs321114287/p/4680023.html
Copyright © 2020-2023  润新知