Associative Containers: set, map, multiset, multimap
Associative containers in C++ store and retrieve elements efficiently using keys. Unlike sequence containers, they keep elements automatically sorted (usually with the less-than operator) and include types like std::set, std::map, std::multiset, and std::multimap.
Associative containers guarantee logarithmic time complexity for lookup, insertion, and removal by using balanced binary search trees internally. Each container has unique characteristics:
- In a
std::set, each key is unique, ensuring no duplicates; - A
std::mapassociates each unique key with a single value; - A
std::multisetallows duplicate keys, making it useful when multiple identical elements are needed; - A
std::multimaplets you associate multiple values with the same key.
set_example.cpp
map_example.cpp
1234567891011#include <iostream> #include <set> int main() { std::set<int> s; s.insert(3); s.insert(1); s.insert(2); s.insert(2); // Duplicate, will be ignored }
While std::set and std::map enforce key uniqueness, you sometimes need to store multiple identical keys. Use std::multiset and std::multimap for these situations.
main.cpp
123456789101112131415161718192021222324252627#include <iostream> #include <map> #include <string> int main() { // Create a multimap to store names and their grades std::multimap<std::string, int> grades; // Insert multiple grades for the same names grades.insert({"Alice", 85}); grades.insert({"Bob", 90}); grades.insert({"Alice", 92}); grades.insert({"Bob", 78}); // Print all grades for Alice std::cout << "All grades for Alice:\n"; auto range = grades.equal_range("Alice"); // Get range of elements with key "Alice" for (auto it = range.first; it != range.second; ++it) std::cout << it->first << " => " << it->second << "\n"; // Print all entries in the multimap std::cout << "\nAll entries in multimap:\n"; for (const auto& [name, score] : grades) std::cout << name << " => " << score << "\n"; }
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 6.67
Associative Containers: set, map, multiset, multimap
Veeg om het menu te tonen
Associative containers in C++ store and retrieve elements efficiently using keys. Unlike sequence containers, they keep elements automatically sorted (usually with the less-than operator) and include types like std::set, std::map, std::multiset, and std::multimap.
Associative containers guarantee logarithmic time complexity for lookup, insertion, and removal by using balanced binary search trees internally. Each container has unique characteristics:
- In a
std::set, each key is unique, ensuring no duplicates; - A
std::mapassociates each unique key with a single value; - A
std::multisetallows duplicate keys, making it useful when multiple identical elements are needed; - A
std::multimaplets you associate multiple values with the same key.
set_example.cpp
map_example.cpp
1234567891011#include <iostream> #include <set> int main() { std::set<int> s; s.insert(3); s.insert(1); s.insert(2); s.insert(2); // Duplicate, will be ignored }
While std::set and std::map enforce key uniqueness, you sometimes need to store multiple identical keys. Use std::multiset and std::multimap for these situations.
main.cpp
123456789101112131415161718192021222324252627#include <iostream> #include <map> #include <string> int main() { // Create a multimap to store names and their grades std::multimap<std::string, int> grades; // Insert multiple grades for the same names grades.insert({"Alice", 85}); grades.insert({"Bob", 90}); grades.insert({"Alice", 92}); grades.insert({"Bob", 78}); // Print all grades for Alice std::cout << "All grades for Alice:\n"; auto range = grades.equal_range("Alice"); // Get range of elements with key "Alice" for (auto it = range.first; it != range.second; ++it) std::cout << it->first << " => " << it->second << "\n"; // Print all entries in the multimap std::cout << "\nAll entries in multimap:\n"; for (const auto& [name, score] : grades) std::cout << name << " => " << score << "\n"; }
Bedankt voor je feedback!