关联容器 | 是否有序 | 是否关联值 | 是否可重复 | 访问时间 |
---|---|---|---|---|
set | 是 | 否 | 否 | 对数 |
map | 是 | 是 | 否 | 对数 |
multiset | 是 | 否 | 是 | 对数 |
multimap | 是 | 是 | 是 | 对数 |
unordered_map | 否 | 是 | 否 | 常数 |
unordered_set | 否 | 否 | 否 | 常数 |
unordered_multiset | 否 | 否 | 是 | 常数 |
unordered_multimap | 否 | 是 | 是 | 常数 |
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <array>
using namespace std;
template <typename AssozCont>
bool containsElement5(const AssozCont& assozCont) {
return assozCont.contains(5);
}
int main() {
// std::map
std::map<std::string, int> m{ {"Dijkstra", 1972}, {"Scott", 1976} };
m["Ritchie"] = 1983;
std::cout << m["Ritchie"]; // 1983
for (auto p : m) std::cout << "{" << p.first << "," << p.second << "}";
// {Dijkstra,1972},{Ritchie,1983},{Scott,1976}
m.erase("Scott");
for (auto p : m) std::cout << "{" << p.first << "," << p.second << "}";
// {Dijkstra,1972},{Ritchie,1983}
m.clear();
std::cout << m.size() << std::endl; // 0
// std::unordered_map
std::unordered_map<std::string, int> um{ {"Dijkstra", 1972}, {"Scott", 1976} };
um["Ritchie"] = 1983;
std::cout << um["Ritchie"]; // 1983
for (auto p : um) std::cout << "{" << p.first << "," << p.second << "}";
// {Ritchie,1983},{Scott,1976},{Dijkstra,1972}
um.erase("Scott");
for (auto p : um) std::cout << "{" << p.first << "," << p.second << "}";
// {Ritchie,1983},{Dijkstra,1972}
um.clear();
std::cout << um.size() << std::endl; // 0
// std::set
std::set<int> mySet{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::cout << "containsElement5(mySet): "
<< containsElement5(mySet); // true
//std::unordered_set
std::unordered_set<int> myUnordSet{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
std::cout << "containsElement5(myUnordSet): "
<< containsElement5(myUnordSet); // true
// std::multiset
std::multiset<int> myMuliSet{ 3, 1, 5, 3, 4, 5, 1, 4, 4, 3, 2, 2, 7, 6, 4, 3, 6 };
for (auto s : myMuliSet) std::cout << s << " ";
// 1 1 2 2 3 3 3 3 4 4 4 4 5 5 6 6 7
myMuliSet.insert(8);
std::array<int, 5> myArr{ 10, 11, 12, 13, 14 };
myMuliSet.insert(myArr.begin(), myArr.begin() + 3);
myMuliSet.insert({ 22, 21, 20 });
for (auto s : myMuliSet) std::cout << s << " ";
// 1 1 2 2 3 3 3 3 4 4 4 4 5 5 6 6 7 10 11 12 20 21 22
std::cout << myMuliSet.erase(4); // 4
myMuliSet.erase(myMuliSet.lower_bound(5), myMuliSet.upper_bound(15));
for (auto s : myMuliSet) std::cout << s << " ";
// 1 1 2 2 3 3 3 3 20 21 22
// std::multimap
std::multimap<char, int> multiMap = { {'a', 10}, {'a', 20}, {'b', 30} };
for (auto p : multiMap) std::cout << "{" << p.first << "," << p.second << "} ";
// {a,10} {a,20} {b,30}
return 0;
}
标签:std,map,set,cout,myMuliSet,include,unordered
From: https://blog.csdn.net/zg260/article/details/139906026