C++ का unordered_map
In this page:
#include <unordered_map>
std::unordered_map<key_type, value_type> map_name;
map_name[key] = value;
map_name.at(key);
unordered_map क्या है?
std::unordered_map एक associative container है जो key-value pairs को बिना किसी guaranteed order के store करता है, एक hash table की तरह implemented।
Average-case lookup, insertion, और deletion O(1) time में चलते हैं, sorted keys की ज़रूरत न रखने वाले ज़्यादातर workloads के लिए map से तेज़।
उदाहरण: What is unordered_map?
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::unordered_map<std::string, int> ages;
ages["Alex"] = 30;
// Print to the console with cout
std::cout << ages["Alex"] << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Elements Access करना
map की तरह, unordered_map access के लिए operator[] और at() support करता है। क्योंकि कोई ordering नहीं है, elements purely key से retrieve होते हैं, position से नहीं।
उदाहरण: Accessing Elements
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::unordered_map<std::string, int> ages = {{"Alex", 30}};
// Print to the console with cout
std::cout << ages.at("Alex") << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Hashing और Performance
unordered_map इसका bucket determine करने के लिए हर key hash करता है। यह average O(1) operations देता है लेकिन अगर कई keys collide करें worst-case O(n)। bucket_count() और load_factor() internal hash table state expose करते हैं।
उदाहरण: Hashing & Performance
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m;
m[1] = 10;
// Print to the console with cout
std::cout << "buckets=" << m.bucket_count() << " load_factor=" << m.load_factor() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterating (Unordered)
एक unordered_map iterate करना elements को एक unspecified, implementation-defined order में visit करता है जो insertions के बीच बदल सकता है। correctness के लिए कभी iteration order पर depend न करें।
उदाहरण: Iterating (Unordered)
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m = {{1, 10}, {2, 20}};
for (const auto &[key, value] : m) {
// Print to the console with cout
std::cout << key << ":" << value << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Search और Erase करना
find(), count(), और erase() std::map जैसे ही काम करते हैं, लेकिन tree traversal की बजाय hashing पर depend करते हैं, उन्हें बड़े unsorted datasets पर average से तेज़ बनाते हुए।
उदाहरण: Searching and Erasing
// Include std::cout and std::cin
#include <iostream>
#include <unordered_map>
// Program execution starts in main()
int main() {
std::unordered_map<int, int> m = {{1, 10}};
// Declare it and set it to m.find(1)
auto it = m.find(1);
if (it != m.end()) std::cout << "Found: " << it->second << std::endl;
m.erase(1);
// Print to the console with cout
std::cout << m.size() << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
unordered_mapsorted या insertion order में iterate करेगा। - existence test करने के लिए
m[key]इस्तेमाल करना, जो एक default entry insert करता है। - एक hash function और equality दिए बिना एक custom key type इस्तेमाल करना।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: