STL:unordered_map使用ノート

1937 ワード

The swap functions do not invalidate any of the iterators inside the container, but they do invalidate the iterator marking the end of the swap region. References and pointers to either key or data stored in the container are only invalidated by erasing that element, even when the corresponding iterator is invalidated.
スワップ関数は、コンテナ内の反復器を無効にすることはありませんが、スワップ領域の終了をマークする反復器を無効にします.
コンテナに格納されているキーまたはデータへの参照とポインタは、対応する反復器が無効であっても、要素を消去することによってのみ無効になります.
次は、あるドキュメントに従って手当たり次第に叩いて、練習するdemoです.しばらくして底のハッシュバケツをまとめます.
使用とmapの差は多くありません.主に無秩序なハッシュのため、メモリの消費量が少なくなります.
void test_map()
{
	std::unordered_map letter_counts{ {'a', 27}, {'b', 3}, {'c', 1} };
	auto it = letter_counts.find('c');
	letter_counts.insert(make_pair('d', 6));
	letter_counts['e'] = 10;
	letter_counts.insert(unordered_map::value_type('f', 10));
	unordered_map::iterator its = letter_counts.begin();
	int s= letter_counts.count('s');
	int a = letter_counts.count('a');
	//letter_counts.erase(it);
	cout << s << a<  word_map;
	for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
						   "this", "sentence", "is", "a", "hoax" }) {
		++word_map[w];
	}

	for (const auto &pair : word_map) {
		std::cout << pair.second << " occurrences of word '" << pair.first << "'
"; } }