• STL库常用数据结构用法


    介绍了map、vector、queue、set的使用。以及string与char【】的互相转换

    #include <iostream>
    #include <map>
    #include <set>
    #include <string>
    #include <unordered_map>
    #include <unordered_set>
    #include <vector>
    #include <queue>
    using namespace std;
    
    void vectorTest() {
        struct {
            vector<int> m;
            int start, end, day;
        } teams[999];
    
        // add
        teams[0].m.insert(teams[0].m.begin() + 2, 2);
        teams[0].m.push_back(1);
    
        // del
        teams[0].m.clear();
        teams[0].m.erase(teams[0].m.begin() + 1, teams[0].m.end());
    
        teams[0].m.size();
    
        // query,update
        teams[0].m[0] = 0;
        int qwq = teams[0].m[0];
    
        //遍历
        for (auto it = teams[0].m.begin(); it != teams[0].m.end(); it++) {
            printf("%d\n", *it);
        }
        for (vector<int>::iterator it = teams[0].m.begin(); it != teams[0].m.end();
             it++) {
            printf("%d\n", *it);
        }
    }
    
    /*
        Map底层是红黑树,根据key排序
        unorderMap底层是哈希表,建议使用unorderMap。
    */
    void mapTest() {
        unordered_map<string, int> strMap;
    
        // add
        char a[10] = "qwq";
        strMap.insert(make_pair("123", 2));
        strMap.insert(make_pair(a, 4));
    
        // update
        strMap[a] = 3;
        int test = strMap[a];
    
        // query
        for (auto it = strMap.begin(); it != strMap.end(); it++) {
            cout << it->first << " " << it->second << endl;
        }
    
        // del
        strMap.erase(strMap.find("123")); //根据key查找
        // strMap.clear();
    }
    
    /*
        底层实现是红黑树,mutiset元素可重复
    */
    vector<int> testVector;
    void setTest()
    {
        testVector.push_back(1);//0
        testVector.push_back(5);//1
        testVector.push_back(2);//2
        testVector.push_back(3);//3
        testVector.push_back(4);//4
        //1,4,3,2,0
        struct cmp{
          bool operator()(const int &a,const int &b)
          {
              return testVector[a]>testVector[b];
          }  
        };
        set<int,cmp> s;
    
        //add
        s.insert(1);
        s.insert(0);
        s.insert(2);
        s.insert(3);
        s.insert(4);
    
        //query
        for(auto it=s.begin();it!=s.end();it++)
        {
            cout<<*it<<endl;
        }
        cout<<*s.find(123)<<endl;
    
        //del
        s.erase(s.find(0));
    
        //no update
    }
    
    /*
        优先队列,底层实现为堆,也可以用set实现。性能上在不同场景有较小差异。
    */
    void queueTest()
    {
        struct queueContent
        {   
            int num;
            queueContent(int num)
            {
                this->num = num;
            }
            bool operator<(const queueContent &a)const
            {
                return num<a.num; //堆顶为最大值
            }
        };
        priority_queue<queueContent> q;
        q.push(queueContent(1));
        q.push(queueContent(2));
        q.push(queueContent(3));
    
        while(!q.empty())
        {
            queueContent tmp = q.top();
            q.pop();
            cout<<tmp.num<<endl;
        }
        
    }
    
    
    void stringAndchar() {
        // String to char[]
        string pp = "dagah";
        char p[8];
        int i;
        for (i = 0; i < pp.length(); i++)
            p[i] = pp[i];
        p[i] = '\0';
        printf("%s\n", p);
        cout << p;
    
        //char[] to string
        char test[10]="qweqweasd";
        string test2 = test;
    }
    
    
    int main() { queueTest(); }
  • 相关阅读:
    YAOI Summer Round #4 (Div.2) 题解
    2021 暑假集训(福建师大附中)
    YAOI Round #7 题解
    YAOI Round #5 题解
    插头DP
    四边形不等式
    YAOI Round #3 题解
    关于 2020 年
    图论相关性质和结论(基础)
    斜率优化 DP :Luogu P2365 P5785「SDOI2012」任务安排 & 弱化版
  • 原文地址:https://www.cnblogs.com/MiraculousB/p/16589091.html
Copyright © 2020-2023  润新知