STL-multiset(ACM)

发布时间 2023-06-11 16:29:10作者: AC玴

1.与set不同的是,multiset可以允许多个相同元素同时出现

重载函数(默认)

multiset<int, int> mu;

基本操作

mu.erase(x); // 把所有与x相同的元素删除

// 如果我们只想删除一个的话
// 通过删除迭代器实现
mu.erase(mu.find(x));
mu.count(x); // 查的时间与x的个数有关,慎用

需要查询很多数据存在的次数时(用map来存)

map<int, int> m;
vector<int> v{1, 2, 4, 4, 4, 5, 6, 54, 4, 8};
for (int x : v) {
    m[x] ++;
}
for (auto x : m) {
    cout << x.first << ": " << x.second << endl;
}
// 结果
1: 1
2: 1
4: 4
5: 1
6: 1
8: 1
54: 1