main.cpp
// g++ main.cpp -O2 -std=c++17
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
int main() {
/* std::unordered_set */
std::unordered_set<int> s1{2,13,3,5,8,21,1,32};
std::unordered_set<int> s2{2,8,21,1,13,3,5,32};
for(const auto&it: s1){
std::cout << it << " ";
}
std::cout << std::endl;
std::cout << s1.bucket_count() << std::endl;
std::cout << s2.bucket_count() << std::endl;
std::cout << (s2 == s1) << std::endl;
std::cout << std::string(40, '-') << std::endl;
/* std::unordered_map */
std::unordered_map<std::string, int> m1{{"MAC", 7999},{"Cellphone", 1499},{"Water", 9},{"KFC", 49}};
for(const auto& [k, v]: m1){ // structured bindings
std::cout << k << ": " << v << std::endl;
}
std::cout << m1.bucket_size(10) << std::endl;
}
输出:
32 1 21 8 5 3 13 2
13
13
1
----------------------------------------
KFC: 49
Water: 9
Cellphone: 1499
MAC: 7999
1