- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How we can translate Python dictionary into C++?
A python dictionary is a Hashmap. You can use the map data structure in C++ to mimic the behavior of a python dict. You can use map in C++ as follows:
#include <iostream> #include <map> using namespace std; int main(void) { /* Initializer_list constructor */ map<char, int> m1 = { {'a', 1}, {'b', 2}, {'c', 3}, {'d', 4}, {'e', 5} }; cout << "Map contains following elements" << endl; for (auto it = m1.begin(); it != m1.end(); ++it) cout << it->first << " = " << it->second << endl; return 0; }
This will give the output
The map contains following elements a = 1 b = 2 c = 3 d = 4 e = 5
Note that this map is equivalent to the python dict:
m1 = { 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5 }
Advertisements