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

C++ में map और multimap

एक map एक key और एक value के pairs store करता है, एक phone book की तरह जहाँ आप नाम से एक number ढूंढते हैं। यह keys को sorted order में रखता है, और multimap repeated keys allow करता है।
Syntax
cpp
#include <map>

std::map<key_type, value_type> map_name;
map_name[key] = value;
map_name.at(key);

map क्या है?

std::map एक associative container है जो key-value pairs को key से sorted order में store करता है, internally एक balanced binary search tree (typically red-black tree) की तरह implemented। Keys unique हैं और lookups, insertions, और deletions O(log n) time में चलते हैं।

उदाहरण: What is a map?

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::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;
}

Values Access और Update करना

आप operator[] से values access कर सकते हैं (जो key missing होने पर एक default value insert करता है) या at() से (जो missing key के लिए out_of_range throw करता है)। एक existing key को assign करना इसकी value update करता है।

उदाहरण: Accessing and Updating Values

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages;
	ages["Alex"] = 30;
	ages["Alex"] = 31;
	// Print to the console with cout
	std::cout << ages.at("Alex") << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

map बनाम multimap

std::map को unique keys चाहिए -- एक duplicate key insert करना operator[] के जरिए existing value overwrite कर देता है। std::multimap same key वाली कई entries allow करता है, उन्हें sorted order में adjacently store करते हुए।

उदाहरण: map vs multimap

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>

// Program execution starts in main()
int main() {
	std::map<int, int> m;
	m[1] = 10;
	m[1] = 20;
	// Print to the console with cout
	std::cout << m[1] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक map पर Iterate करना

एक map iterate करना key-value pairs को ascending key order में visit करता है। हर element एक std::pair है, .first (key) और .second (value) से accessed, या C++17 में structured bindings से।

उदाहरण: Iterating over a map

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages = {{"Alex", 30}, {"Sam", 25}};
	for (const auto &[name, age] : ages) {
		// Print to the console with cout
		std::cout << name << ":" << age << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Search और Erase करना

find() element का एक iterator return करता है या अगर key missing हो तो end()। count() 1 (map) या matches की संख्या (multimap) return करता है। erase() key या iterator से एक entry remove करता है।

उदाहरण: Searching and Erasing

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::map
#include <map>
#include <string>

// Program execution starts in main()
int main() {
	std::map<std::string, int> ages = {{"Alex", 30}};
	// Declare it and set it to ages.find("Alex")
	auto it = ages.find("Alex");
	if (it != ages.end()) std::cout << "Found: " << it->second << std::endl;
	ages.erase("Alex");
	// Print to the console with cout
	std::cout << ages.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. एक key check करने के लिए m[key] इस्तेमाल करना, जो अगर यह missing हो तो एक default value insert करता है; find या count इस्तेमाल करें।
  2. यह उम्मीद करना कि map insertion order रखेगा, जब यह key से sorted है।
  3. एक const map पर [] call करना, जो compile नहीं होता।

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.