Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Associative Containers: set, map, multiset, multimap | Containers
C++ STL Containers and Algorithms

bookAssociative Containers: set, map, multiset, multimap

Note
Definition

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::map associates each unique key with a single value;
  • A std::multiset allows duplicate keys, making it useful when multiple identical elements are needed;
  • A std::multimap lets you associate multiple values with the same key.
set_example.cpp

set_example.cpp

map_example.cpp

map_example.cpp

copy
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

main.cpp

copy
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"; }
question mark

Which statement correctly describes a key characteristic of associative containers in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain the difference between a set and a multiset with examples?

When should I use a multimap instead of a map?

Are there any performance differences between these containers?

Awesome!

Completion rate improved to 6.67

bookAssociative Containers: set, map, multiset, multimap

Svep för att visa menyn

Note
Definition

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::map associates each unique key with a single value;
  • A std::multiset allows duplicate keys, making it useful when multiple identical elements are needed;
  • A std::multimap lets you associate multiple values with the same key.
set_example.cpp

set_example.cpp

map_example.cpp

map_example.cpp

copy
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

main.cpp

copy
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"; }
question mark

Which statement correctly describes a key characteristic of associative containers in C++?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt