允许重复: 我们在讨论中讨论了无序的_图 以前的职位 ,但有一个限制,我们不能在无序_映射中存储重复项,也就是说,如果在无序_多重映射中已经有一个键值对,并且插入了另一对键值对,那么这两个键值都将存在,而在无序_映射的情况下,键值对应的前一个值将被只存在的新值更新。甚至可以在无序的多重映射中存在两次。
内部代表: 无序_多重映射的内部实现与无序_映射的内部实现相同,但对于重复键,每个键值对都会保留另一个计数值。由于对存储在哈希表中,它们之间没有特定的顺序,但具有相同密钥的对在数据结构中聚集在一起,而具有相同值的对不保证聚集在一起。
时间复杂性: 无序_multimap上的所有操作平均花费恒定的时间,但在最坏的情况下,时间可能会变为线性,这取决于内部使用的哈希函数,但从长远来看,无序_multimap优于multimap(基于树的multimap)。
功能: unordered_multimap支持以下代码中演示的许多功能:
CPP
// C++ program to demonstrate various function of // unordered_multimap #include <bits/stdc++.h> using namespace std; // making typedef for short declaration typedef unordered_multimap<string, int >::iterator unit; // Utility function to print unordered_multimap void printUmm(unordered_multimap<string, int > umm) { // begin() returns iterator to first element of map unit it = umm.begin(); for (; it != umm.end(); it++) cout << "<" << it->first << ", " << it->second << "> " ; cout << endl; } // Driver code int main() { // empty initialization unordered_multimap<string, int > umm1; // Initialization bu intializer list unordered_multimap<string, int > umm2( { { "apple" , 1 }, { "ball" , 2 }, { "apple" , 10 }, { "cat" , 7 }, { "dog" , 9 }, { "cat" , 6 }, { "apple" , 1 } }); // Initialization by assignment operation umm1 = umm2; printUmm(umm1); // empty returns true, if container is empty else it // returns false if (umm2.empty()) cout << "unordered multimap 2 is empty" ; else cout << "unordered multimap 2 is not empty" ; // size returns total number of elements in container cout << "Size of unordered multimap 1 is " << umm1.size() << endl; string key = "apple" ; // find and return any pair, associated with key unit it = umm1.find(key); if (it != umm1.end()) { cout << "key " << key << " is there in unordered " << " multimap 1" ; cout << "one of the value associated with " << key << " is " << it->second << endl; } else cout << "key " << key << " is not there in unordered" << " multimap 1" ; // count returns count of total number of pair // associated with key int cnt = umm1.count(key); cout << "total values associated with " << key << " are " << cnt << "" ; printUmm(umm2); // one insertion by making pair explicitly umm2.insert(make_pair( "dog" , 11)); // insertion by initializer list umm2.insert({ { "alpha" , 12 }, { "beta" , 33 } }); cout << "After insertion of <alpha, 12> and <beta, " "33>" ; printUmm(umm2); // erase deletes all pairs corresponding to key umm2.erase( "apple" ); cout << "After deletion of apple" ; printUmm(umm2); // clear deletes all pairs from container umm1.clear(); umm2.clear(); if (umm2.empty()) cout << "unordered multimap 2 is empty" ; else cout << "unordered multimap 2 is not empty" ; } |
<dog, 9> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 10> <apple, 1> unordered multimap 2 is not emptySize of unordered multimap 1 is 7key apple is there in unordered multimap 1one of the value associated with apple is 1total values associated with apple are 3<dog, 9> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 10> <apple, 1> After insertion of <alpha, 12> and <beta, 33><alpha, 12> <dog, 11> <dog, 9> <beta, 33> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 10> <apple, 1> After deletion of apple<alpha, 12> <dog, 11> <dog, 9> <beta, 33> <cat, 6> <cat, 7> <ball, 2> unordered multimap 2 is empty
正如我们在上面的代码中看到的,大多数操作类似于无序的_图,但需要注意的是: 我们可以使用初始值设定项列表一次初始化和插入许多对。 无序的_multimap没有[]运算符,因为对应于一个键的值不是唯一的,可能有许多值与单个键关联,因此[]运算符不能应用于它们。 擦除功能删除与提供的键关联的值的所有实例。 Find函数将迭代器返回到与该键关联的所有对中的任何键值对实例。
如何访问/删除密钥的特定值? 如果我们想检查是否存在一个特定的,我们需要循环对应于k的所有键值对,以类似的方式,我们可以从数据结构中删除一个特定的副本。键的所有值都没有指定的存储顺序。
CPP
// C++ program to demonstrate various function of // unordered_multimap #include <bits/stdc++.h> using namespace std; // making typedef for short declaration typedef unordered_multimap<string, int >::iterator unit; // Utility function to print unordered_multimap void printUmm(unordered_multimap<string, int > umm) { // begin() returns iterator to first element of map unit it = umm.begin(); for (; it != umm.end(); it++) cout << "<" << it->first << ", " << it->second << "> " ; cout << endl; } // Driver code int main() { // empty initialization unordered_multimap<string, int > umm1; // Initialization bu intializer list unordered_multimap<string, int > umm2( { { "apple" , 1 }, { "ball" , 2 }, { "apple" , 10 }, { "cat" , 7 }, { "dog" , 9 }, { "cat" , 6 }, { "apple" , 1 } }); // Initialization by assignment operation umm1 = umm2; printUmm(umm1); // empty returns true, if container is empty else it // returns false if (umm2.empty()) cout << "unordered multimap 2 is empty" ; else cout << "unordered multimap 2 is not empty" ; // size returns total number of elements in container cout << "Size of unordered multimap 1 is " << umm1.size() << endl; string key = "apple" ; // find and return any pair, associated with key unit it = umm1.find(key); if (it != umm1.end()) { cout << "key " << key << " is there in unordered " << " multimap 1" ; cout << "one of the value associated with " << key << " is " << it->second << endl; } else cout << "key " << key << " is not there in unordered" << " multimap 1" ; // count returns count of total number of pair // associated with key int cnt = umm1.count(key); cout << "total values associated with " << key << " are " << cnt << "" ; printUmm(umm2); // one insertion by making pair explicitly umm2.insert(make_pair( "dog" , 11)); // insertion by initializer list umm2.insert({ { "alpha" , 12 }, { "beta" , 33 } }); cout << "After insertion of <alpha, 12> and <beta, " "33>" ; printUmm(umm2); // erase deletes all pairs corresponding to key umm2.erase( "apple" ); cout << "After deletion of apple" ; printUmm(umm2); // clear deletes all pairs from container umm1.clear(); umm2.clear(); if (umm2.empty()) cout << "unordered multimap 2 is empty" ; else cout << "unordered multimap 2 is not empty" ; } |
Initial content<dog, 9> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 10> <apple, 1> After insertion of one more <apple, 1><dog, 9> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 1> <apple, 10> <apple, 1> After deletion one occurrence of <apple, 1><dog, 9> <cat, 6> <cat, 7> <ball, 2> <apple, 1> <apple, 10> <apple, 1>
无序_多重映射的方法:
- 开始 –返回一个迭代器,该迭代器指向容器中的第一个元素或容器中的第一个元素。
- 结束() –返回一个迭代器,该迭代器指向容器中最后一个元素之后的位置,或指向容器中最后一个元素之后的位置。
- 计数() –返回容器中键等于参数中传递的键的元素数。
- cbegin() –返回一个常量迭代器,该迭代器指向容器中的第一个元素或容器中的第一个元素。
- cend() –返回一个常量迭代器,该迭代器指向容器中最后一个元素之后的位置,或指向容器中最后一个元素之后的位置。
- 清除() –清除无序的_multimap容器的内容。
- 大小() –返回无序的_多重映射的大小。它表示容器中元素的数量。
- 互换 –交换两个无序的_multimap容器的内容。两种容器的尺寸可能不同。
- 查找() –返回一个迭代器,该迭代器指向键为k的元素之一。
- 水桶大小() –返回bucket n中的元素数。
- 空的() –如果无序的_multimap容器为空,则返回true。否则,返回false。
- 等_范围() –返回元素的所有键等于一个键的范围。
- 接线员= –从不同容器中复制/分配/移动元素。
- 最大尺寸() –返回无序的_multimap容器可以容纳的最大元素数。
- 荷载系数() –返回无序的_multimap容器中的当前负载系数。
- 键_eq() –根据比较返回布尔值。
- 安置() –在无序的_multimap容器中插入一个新的{key,element}。
- 安放_hint() –在无序的_multimap容器中插入一个新的{key:element}。
- 桶数 –返回无序的_multimap容器中的桶总数。
- 桶() –返回指定密钥所在的存储桶编号。
- 最大载荷系数() –返回无序的_multimap容器的最大负载系数。
- rehash() –将容器中的桶数设置为N或更多。
- 储备 –将容器中的桶数(桶计数)设置为最合适的数字,以便容器至少包含n个元素。
- hash_函数() –该散列函数是一元函数,仅接受一个参数,并基于该参数返回一个唯一的size_t类型值。
- 最大桶数 –返回无序多重映射容器可以拥有的最大存储桶数。
最近关于无序多图的文章 本文由Utkarsh Trivedi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论