无序关联式容器 unordered_map
http://c.biancheng.net/view/7231.html
1 2
| #include <unordered_map> using namespace std;
|
1 2 3 4 5 6
| template < class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, class Alloc = allocator< pair<const Key,T> > > class unordered_map;
|
1 2 3 4 5
| unordered_map<string, string> umap; unordered_map<string, string> umap{{"a", "1"}, {"b", "2"}}; unordered_map<string, string> umap(umap2); unordered_map<string, string> umap(umap2.begin(), ump2.begin());
|
方法同关联式容器map~
无序关联式容器 unordered_set
http://c.biancheng.net/view/7250.html
1 2
| #include <unordered_set> using namespace std;
|
1 2 3 4 5
| template < class Key, class Hash = hash<Key>, class Pred = equal_to<Key>, class Alloc = allocator<Key> > class unordered_set;
|
1 2
| unordered_set<int> us; unordered_set<int> us{2, 4, 6};
|
方法同set~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| struct Node { int x, y; Node(int x=0, int y=0) : x(x), y(y) {}
};
struct cmp { bool operator()(const Node &p1, const Node &p2) const { return p1.x == p2.x and p1.y == p2.y; } };
struct hashnode { size_t operator()(const Node& p1) const { return hash<int>()(p1.x) ^ hash<int>()(p1.y); } };
void test_unordered_set2() { unordered_set<Node, hashnode, cmp> us; us.insert(Node(2, 2)); us.insert(Node(6, 4)); us.insert(Node(4, 3)); us.insert(Node(9, 1));
if (us.find(Node(2, 2)) != us.end()) { cout << "find\n"; } if (us.count(Node(2, 2)) == 1) { cout << "find\n"; }
for (auto & v: us) { cout << v.x << " " << v.y << " "; } }
|