跳转至

其他

multiset多重集合

//multiset里面,每个元素可以存多个
multiset<int> s;
s.insert(5);
s.insert(5);
s.insert(5);
cout << s.count(5) << "\n"; // 3

//把5这个元素全删了
s.erase(5);
cout << s.count(5) << "\n"; // 0
//只删掉一个5元素
s.erase(s.find(5));
cout << s.count(5) << "\n"; // 2

priority_queue优先队列

Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.

This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).

priority_queue<int> q;
q.push(3);
q.push(5);
q.push(7);
q.push(2);
cout << q.top() << "\n"; // 7
q.pop();
cout << q.top() << "\n"; // 5
q.pop();
q.push(6);
cout << q.top() << "\n"; // 6
q.pop();

// 默认是大根堆,如果要写小根堆,就是push x进去的时候,push成-x,
// 取出使用的时候,加个负号再使用。

// 实现大根堆,还有一个方法,如下:
priority_queue<int,vector<int>,greater<int> >q;//这样就可以实现小根堆了

参考:https://www.cnblogs.com/zwfymqz/p/7800654.html, 如何实现自定义结构体的排序

bitset

A bitset stores bits (elements with only two possible values: 0 or 1, true or false, ...).

#include <bits/stdc++.h>

using namespace std;

int main() {
    bitset<16> a; // 0000000000000000
    bitset<16> b(0xfa2); // 0000111110100010
    bitset<16> c(string("0101111001")); // 0000000101111001


    bitset<4> bt;
    bt[1] = 1;  // 中括号访问

    cout << bt.count() << '\n'; // 输出1的个数
    cout << bt.test(1); // 测试第1位是不是1

    if (bt.any()) cout << "have ones." << '\n'; // 判断有没有1
    if (bt.all()) cout << "all ones." << '\n'; // 判断是不是都是1

    bitset<4> foo;
    foo.set();  // 都置成1, 1111
    foo.set(2, 0); // 把第2位置成0, 1011 (从低位到高位3210,这样位置)
    foo.set(2); // 把第2位置成1, 1111

    foo.reset(1); // 把第1位置成0
    foo.reset(); // 都置成0

    foo.flip(2);
    foo.flip(); // 翻转所有位置

    foo.to_string(); // 转换成字符串
    foo.to_ullong(); // 转换成无符号长整型

    return 0;
}
// 通过打表预处理,实现输入一个八位有符号二进制,输出对应的十进制
#include<bits/stdc++.h>

using namespace std;

map<string, int> mp;

void init() {
        for (int i = -128; i < 128; i++) {
                bitset<8> bit(i);
                mp[bit.to_string()] = i;
        }
}

int main() {
        init();

        string s;
        cin >> s;
        cout << mp[s] << '\n';

        return 0;
}

array

Arrays are fixed-size sequence containers: they hold a specific number of elements ordered in a strict linear sequence.

array<int, 3> e;

algorithm 头文件常用函数

reverse(first, last) 
// 翻转左闭右开区间[first, last)内的所有元素

next_permutation(first, last) 
// 把[first, last)这个区间内的元素,排列成,字典序中,比当前序列更大的下一个排列
// 下一个更大的排列,返回true
// 没有下一个更大的排列了,就返回false

prev_permutation(first, last)
// 把[first, last)这个区间内的元素,排列成,字典序中,比当前序列更小的上一个排列

lower_bound(first, last, value)
// 在[first, last)这个区间,找第一个 >= value的位置,必须有序

upper_bound(first, last, value)
// 在[first, last)这个区间,找第一个 > value的位置,必须有序

next_permutation 示范代码

#include <bits/stdc++.h>

using namespace std;

int main(){
    vector<int> A = {1, 2, 3};

    do {
        for (auto i : A) cout << i << ' ';
        cout << endl;
    } while (next_permutation(A.begin(), A.end()));

    return 0;
}

总结

vector, set, map, pair, stack, queue, priority_queue, bitset

参考

https://cplusplus.com/reference