Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Iterators in Associative Containers | Iterators and Containers
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ STL Iterators

bookIterators in Associative Containers

Swipe to show menu

Note
Definition

Associative containers store elements identified by keys, with some also storing mapped values. The main types are std::set, std::map, std::unordered_set, and std::unordered_map. Their iterators are bidirectional for ordered containers and forward for unordered ones, providing access to keys or keyโ€“value pairs. Keys cannot be modified through iterators.

When working with associative containers in C++, you interact with elements using iterators that reflect the unique structure of these containers. The most common associative containers are std::set, std::map, std::unordered_set, and std::unordered_map.

The ordering of iteration is guaranteed for ordered containers but not for unordered ones.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425
#include <iostream> #include <map> #include <unordered_set> #include <string> int main() { std::map<std::string, int> ordered_map = { {"apple", 3}, {"banana", 1}, {"cherry", 2} }; std::unordered_set<std::string> unordered_set = { "zebra", "lion", "elephant" }; std::cout << "Iterating over std::map (ordered):\n"; for (const auto& pair : ordered_map) std::cout << pair.first << " => " << pair.second << '\n'; std::cout << "\nIterating over std::unordered_set (unordered):\n"; for (const auto& animal : unordered_set) std::cout << animal << '\n'; }

In the above code, you see two associative containers in action. When iterating over the std::map, the elements are traversed in sorted order by key: "apple", "banana", then "cherry". The iterator for std::map gives you access to both the key (pair.first) and the value (pair.second). In contrast, when iterating over the std::unordered_set, the order of elements is unspecified and may differ between runs, reflecting the lack of ordering guarantees. The iterator for std::unordered_set gives access to the key only, since sets do not store mapped values.

main.cpp

main.cpp

copy
12345678910111213141516
#include <iostream> #include <map> int main() { std::map<int, std::string> numbers = { {1, "one"}, {2, "two"} }; auto it = numbers.begin(); // Uncommenting the next line would cause a compile-time error: // it->first = 99; std::cout << "Key: " << it->first << ", Value: " << it->second << '\n'; }

This code highlights an important rule of associative containers: keys cannot be modified through iterators. Attempting to change a key results in a compilation error, which protects the containerโ€™s ordering or hashing guarantees.

When iterating over std::map or std::unordered_map, you can access both the key (pair.first) and the value (pair.second), but only the value is mutable. For std::set and std::unordered_set, iterators expose only the key, which is also immutable. This design ensures correctness and consistent performance for all associative containers.

question mark

What is a property of iterators in std::map?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Sectionย 3. Chapterย 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Sectionย 3. Chapterย 2
some-alt