Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 6.67

bookAssociative Containers: set, map, multiset, multimap

Stryg for at vise menuen

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 alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 3
some-alt