← Back to C++ Course | Chapter 13: STL Containers & Algorithms | Lesson 10 of 15

C++ का unordered_map

एक unordered_map key-value pairs एक phone book की तरह store करता है, लेकिन इन्हें किसी order में नहीं रखता। यह एक hashing trick इस्तेमाल करके चीज़ें बहुत जल्दी ढूंढता है।
Syntax
cpp
#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?

cpp
// 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;
}

Elements Access करना

map की तरह, unordered_map access के लिए operator[] और at() support करता है। क्योंकि कोई ordering नहीं है, elements purely key से retrieve होते हैं, position से नहीं।

उदाहरण: Accessing Elements

cpp
// 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;
}

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

cpp
// 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;
}

Iterating (Unordered)

एक unordered_map iterate करना elements को एक unspecified, implementation-defined order में visit करता है जो insertions के बीच बदल सकता है। correctness के लिए कभी iteration order पर depend न करें।

उदाहरण: Iterating (Unordered)

cpp
// 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;
}

Search और Erase करना

find(), count(), और erase() std::map जैसे ही काम करते हैं, लेकिन tree traversal की बजाय hashing पर depend करते हैं, उन्हें बड़े unsorted datasets पर average से तेज़ बनाते हुए।

उदाहरण: Searching and Erasing

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि unordered_map sorted या insertion order में iterate करेगा।
  2. existence test करने के लिए m[key] इस्तेमाल करना, जो एक default entry insert करता है।
  3. एक hash function और equality दिए बिना एक custom key type इस्तेमाल करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.