YouTip LogoYouTip

Cpp Libs Map

In C++, `` is part of the Standard Template Library (STL), providing an associative container for storing key-value pairs. The elements in the `map` container are automatically sorted by key, making it ideal for scenarios requiring fast lookup and ordered data. ### Definition and Features * **Key-Value Pairs**: `map` stores key-value pairs where each key is unique. * **Sorting**: Elements in `map` are automatically sorted by key, typically in ascending order. * **Uniqueness**: Each key can only appear once in the `map`. * **Bidirectional Iterators**: `map` provides bidirectional iterators that can traverse elements forward and backward. ### Basic Syntax Include header file: #include Declare a map container: std::map myMap; * `key_type` is the type of the key. * `value_type` is the type of the value. Insert elements: myMap = value; Access elements: value = myMap; Traverse map: for (std::map::iterator it = myMap.begin(); it != myMap.end(); ++it) { std::cout <first < " <second << std::endl;} For C++11 and above, the traversal can be simplified with a range-based for loop, making the code more concise: for (auto &p : m) { std::cout << p.first << " : " << p.second << std::endl;} ## Example Below is a simple example using `map`. We will create a `map` to store employee names and their ages, and traverse the map to print each employee's name and age. ## Example #include #include #include int main(){ // Create a map container to store employee names and ages std::map employees; // Insert employee information employees=30; employees=25; employees=35; // Traverse the map and print employee information for(std::map::iterator it = employees.begin(); it != employees.end();++it){ std::cout<first <<" is "<second <<" years old."<< std::endl; } return 0; } Output: Alice is 30 years old.Bob is 25 years old.Charlie is 35 years old. ### Advanced Usage Check if a key exists: if (myMap.find(key) != myMap.end()) { // key exists} Remove elements: myMap.erase(key); Clear the map: myMap.clear(); Get the size of the map: size_t size = myMap.size(); Other methods: myMap.empty(); // Check if empty myMap.count("Bob"); // Check if key exists (returns 0 or 1) Custom sorting, default is ascending order, you can use std::greater or a custom comparison function: std::map<int, std::string, std::greater> m; // Descending order Using a custom comparison function: ## Example #include #include #include bool myCompare(const std::string& a, const std::string& b){ return a < b; } int main(){ std::map<std::string, int, std::function> myMap(myCompare); // Other operations... return 0; } `map` is a very useful container in C++ STL, especially suitable for scenarios requiring fast lookup and ordered data. ## Example #include #include #include int main(){ std::map scores; // Insert scores=90; scores=85; scores.insert({"Charlie", 92}); // Traverse for(auto&p : scores){ std::cout<< p.first< "<< p.second<< std::endl; } // Find auto it = scores.find("Bob"); if(it != scores.end()){ std::cout<<"Bob's score: "<second << std::endl; } // Remove scores.erase("Alice"); std::cout<<"Size: "<< scores.size()< 90Bob => 85Charlie => 92Bob's score: 85 Size: 2
← Cpp Libs BitsetCpp Libs Set β†’