set map pair node stack vector string priority_queue

set

元素不重复 自动排序

multiset

元素重复 自动排序

#include <bits/stdc++.h>
using namespace std;
int a[12] = {1,1,2,4,5,6,5,4,3,3,2,1};
set<int> s;
multiset<int> ms,mm;
int main(){
    for(int i = 0; i < 12; i++){
        s.insert(a[i]);
        ms.insert(a[i]);
        if(mm.find(a[i]) == mm.end())//找不到
            mm.insert(a[i]);
    }
    //删除操作 .erase();
    for(auto t : s)
        cout << " " << t;
    puts("");
    for(auto t : ms)
        cout << " " << t;
    puts("");
    for(auto t : mm)
        cout << " " << t;
    return 0;
}

在这里插入图片描述

map

#include <bits/stdc++.h>
using namespace std;
map<int,int> s;//默认排序规则,根据第一个数从小到大(第一个数是唯一的)
int main()
{
    //插入
    s[5]++;
    s[1]++;
    s[6]++;
    s[4]++;
    s[5]++;
    //删除
    s[1]--;
    //查找
    //找到
    if (s[4] != 0)
        cout << "Yes" << endl;
    //找不到
    else cout << "No" << endl;
    //遍历
    for(auto y : s)
        cout << y.first << ' ' << y.second << "\n";
    return 0;
}

输出样例

Yes
1 0
4 1
5 2
6 1

#include <bits/stdc++.h>
using namespace std;
#define int long long
map<char,string> mp;
signed main() {
    ios::sync_with_stdio(0);
    mp['0'] = "0000";mp['1'] = "0001";mp['2'] = "0010";
    mp['A'] = "1010";

    map<char,string> :: iterator it;
    for(it = mp.begin(); it != mp.end();it++)
        cout << it->first << " " << it->second << endl;
    cout << "倒序遍历:\n";
    for(auto t = --mp.end();;t--){
        cout << t->first << " " << t->second << endl;
        if(t == mp.begin()) break;
    }
    cout << "auto的遍历:\n";
    for(auto itt : mp)
        cout << itt.first << " " << itt.second << endl;

    return 0;
}

在这里插入图片描述

multimap

这四种实现原理都是红黑树,查找,插入,删除的时间复杂度事O(logn)

pair

必须是两个元素。这两个类型不必相同,string int vector node

#include <bits/stdc++.h>
using namespace std;
//typedef pair<int,int> pp;
vector<pair<int,int> > ve;
bool cmp1(pair<int,int> a,pair<int,int> b){
    return a.first > b.first;
}
bool cmp2(pair<int,int> a,pair<int,int> b){
    return a.second > b.second;
}
int main(){
    //pp q;
    for(int i = 0; i < 4; i++) {
        int x,y;
        cin >> x >> y;
        ve.push_back({x, y});
    }
    sort(ve.begin(),ve.end(),cmp1);
    cout << "根据cmp1第一个降序得到的结果:\n";
    for(auto t : ve)
        cout << t.first << " " << t.second << "   ";
    puts("");

    sort(ve.begin(),ve.end(),cmp2);
    cout << "根据cmp2第二个降序得到的结果:\n";
    for(auto t : ve)
        cout << t.first << " " << t.second << "   ";
    puts("");
    return 0;
}

在这里插入图片描述

结构体自定义排序

#include <bits/stdc++.h>
using namespace std;
struct node{
    int x;
    bool operator <(const node &a){
        return x > a.x;
    }
};
int main(){
    struct node a[4];
    for(int i = 0; i < 4; i++) {
        a[i].x = i + 1;
    }
    sort(a,a+4);
    for(int i = 0; i < 4; i++)
        cout << a[i].x << " ";
    return 0;
}

stack

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
stack<string>s;
int main()
{
    if(s.empty())
        puts("s is empty");
    s.push("Hello");
    s.push("World");
    s.push("Aaaaa");
    cout << s.top() << endl;
    s.pop();
    cout << s.size() << endl;
    if(s.empty())
        puts("s is empty");
    while(!s.empty()){
        cout << s.top() << " ";
        s.pop();
    }
    return 0;
}

在这里插入图片描述

vector

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> v;
int main()
{
    if(v.empty())
        puts("v is empty");
    for(int i = 0; i < 10; i ++)
        v.push_back(i);
    cout << v.size() << endl;
    v.insert(v.begin() + 9, 10);//在第10个元素前面插入10
    v.push_back(10);//尾部插入10
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    v.erase(v.begin() + 9);//删除第10个
    v.pop_back();//删除末尾
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    reverse(v.begin(),v.end());//反转
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    cout << endl;
    sort(v.begin(),v.end());
    for(int i = 0; i < v.size(); i++)
        cout << v[i] << " ";
    return 0;
}

在这里插入图片描述
v [ n ]
返回 v 中位置为 n 的元素。
push_back() 在数组的最后添加一个数据
pop_back() 去掉数组的最后一个数据
begin() 得到数组头的指针
end() 得到数组的最后一个单元+1的指针
empty() 判断vector是否为空
swap() 与另一个vector交换数据

string

长度 str.length(), str.size()
比较 str1.compare(str2)
查找 pos = str1.find(str2)
连接 str1 += str2
插入 str1.insert(pos,str2)
替换 str1.replace(pos,n,str2)
删除 str1.erase(pos,len)
清除 str.clear()
判空 str.empty()
反转 reverse(s.begin(),s.end())
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
    string str1,str2,str3;
    str1 = "Hello";
    str2 = "World";
    if(str3.empty())//判空
        cout << "str3 is empty" << endl;
    str3 = str1 + str2;//拼接
    cout << str3 << endl;
    cout << str1.size() << " " << str2.size() << " " << str3.size() << endl;//大小
    cout << str1.insert(2,str2) << endl;
    cout << str1 << endl;
    cout << str1.replace(0,1,str2) << endl;//从下标0开始的1个用str2替换
    cout << str3 << endl;
    cout << str3.find("World") << endl;
    cout << str3.erase(2,3)<< endl;
    cout << str3 << endl;
    reverse(str3.begin(),str3.end());
    cout << str3 << endl;



}

vector 和string基本差不多

priority_queue

建立的时间复杂度O(n)
插入删除时间复杂度O(log n)

升序

priority_queue<int,vector<int>,greater<int>> que;

降序

priority_queue<int> que;

与普通队列
同:在队尾插入,队头删除
异:队列中最大元素总是在队头(这也就是说,出队列不是按照先进先出,而是按照元素大小,从大到小出去)

发布了90 篇原创文章 · 获赞 4 · 访问量 9483

猜你喜欢

转载自blog.csdn.net/xcfkaixin/article/details/104136789