C++ में map और multimap
In this page:
#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?
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
एक 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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Search और Erase करना
find() element का एक iterator return करता है या अगर key missing हो तो end()। count() 1 (map) या matches की संख्या (multimap) return करता है। erase() key या iterator से एक entry remove करता है।
उदाहरण: Searching and Erasing
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- एक key check करने के लिए
m[key]इस्तेमाल करना, जो अगर यह missing हो तो एक default value insert करता है;findयाcountइस्तेमाल करें। - यह उम्मीद करना कि
mapinsertion order रखेगा, जब यह key से sorted है। - एक const map पर
[]call करना, जो compile नहीं होता।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first: