
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Iterate Over a Dictionary
Although C++ lacks dictionaries, it does have a structure akin to them called a map. Two values−key and mapped values−are contained in each entry of a map. Each item is indexed using the key value, whereas the mapped values are the values connected to the keys. The mapped values may or may not be unique, but the keys are always unique. In this tutorial, we'll look at iterators and how they work with maps.
Iterators in C++
An iterator object points to one element in a range of elements. Iterators are generally used with arrays and containers like vectors and have a particular set or operations that can be used to point to a specific element in a given range. Iterators point to the memory location of a particular element of a range and can be incremented or decremented to point to a different element present in the range or the container. Let’s take a look at how an iterator works.
Syntax
<container_type> :: iterator iterator_name;
Let’s take an example −
Example
#include <iostream> #include <iterator> #include <vector> using namespace std; int main(){ //we are using a vector to demonstrate the working of an iterator vector<int> myVec = { 10, 20, 30, 40, 50 }; // creating an iterator vector<int>::iterator it; // iterating through the elements cout << "The elements are: "; //the begin and end are used to define the range for (it = myVec.begin(); it < myVec.end(); it++) cout << *it << " "; return 0; }
Output
The elements are: 10 20 30 40 50
Using iterators to iterate through a map
It is a fairly simple process and the same as iterating through other containers.
Syntax
map<datatype, datatype> mmap; for (auto itr = my.begin(); itr != mmap.end(); ++itr) { cout << itr->first << ": " << itr->second << endl; }
Example
#include <iostream> #include <map> using namespace std; int main() { //initialising the map map <string, string> mmap = {{"City", "London"}, {"Country", "UK"}, {"Continent", "Europe"}}; //iterating through the contents for (auto itr = mmap.begin(); itr != mmap.end(); ++itr) { cout << itr->first << ": " << itr->second << endl; } return 0; }
Output
City: London Continent: Europe Country: UK
Conclusion
In C++, maps are deemed as ordered collections, meaning the components are sorted by the values of their key attributes. Red-black trees are used to implement maps in memory, and all operations have a logarithmic time complexity. While iterating through maps, we have to use iterators, or otherwise, there is no other easier way to access all the elements in a map.
- Related Articles
- How to iterate over a C# dictionary?
- Iterate over a dictionary in Python
- How to Iterate over Tuples in Dictionary using Python
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- Golang program to iterate over a Slice
- What is the best way to iterate over a Dictionary in C#?
- Java Program to Iterate over enum
- Haskell Program to Iterate over enum
- Java Program to Iterate over an ArrayList
- C++ Program to Iterate Over an Array
- Swift Program to Iterate Over an Array
- Golang Program to Iterate through Elements of Dictionary
- Python program to iterate over multiple lists simultaneously?
- Program to iterate over a List using Java 8 Lambda
