Home > Backend Development > C++ > C++ program to print dictionary

C++ program to print dictionary

PHPz
Release: 2023-09-11 10:33:02
forward
1145 people have browsed it

C++ program to print dictionary

A map is a special type of container in C, where each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C, we have to use iterators. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range.

Print map in C using iterator

First, let’s look at the syntax of how to define an iterator to print a map.

grammar

map<datatype, datatype> myMap;
map<datatype, datatype > :: iterator it;
for (it = myMap.begin(); it < myMap.end(); it++)
      cout << itr->first << ": " << itr->second << endl;
Copy after login

The alternative is this -

map<datatype, datatype> mmap;
for (auto itr = my.begin(); itr != mmap.end(); ++itr) {
   cout << itr->first << ": " << itr->second << endl;
}
Copy after login

Let’s give an example of using these two methods -

Example

#include <iostream>
#include <map>

using namespace std;

int main() {
   //initialising the map
   map <string, string> mmap = {{"City", "Berlin"}, {"Country", "Germany"}, {"Continent", "Europe"}};
   map <string, string>::iterator itr;

   //iterating through the contents
   for (itr = mmap.begin(); itr != mmap.end(); ++itr) {
      cout << itr->first << ": " << itr->second << endl;
   }
   return 0;
}
Copy after login

Output

City: Berlin
Continent: Europe
Country: Germany
Copy after login

Use the second method -

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;
}
Copy after login

Output

City: London
Continent: Europe
Country: UK
Copy after login

in conclusion

To display the contents of a map in C, we must use an iterator, otherwise it will be difficult to print out the value. Using an iterator makes it easy to iterate through all entries in a map and display their values.

The above is the detailed content of C++ program to print dictionary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template