Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to create something like a python dictionary in C++

Tags:

c++

python

I'm using a struct. Is there some way to iterate through all the items of type "number"?

struct number { int value; string name; };
like image 466
James Wolfe Avatar asked Jan 30 '16 02:01

James Wolfe


1 Answers

In c++ map works like python dictionary, But there is a basic difference in two languages. C++ is typed and python having duck typing. C++ Map is typed and it can't accept any type of (key, value) like python dictionary. A sample code to make it more clear -

  map<int, char> mymap;
  mymap[1] = 'a';
  mymap[4] = 'b';
  cout<<"my map is -"<<mymap[1]<<" "<<mymap[4]<<endl;

You can use tricks to have a map which will accept any type of key, Refer - http://www.cplusplus.com/forum/general/14982/

like image 137
AlokThakur Avatar answered Sep 30 '22 11:09

AlokThakur