hello im a hobbyist who is somewhat new to c++ with lots of experience in python. in python i was able to use dictionaries that could be accessed by keys and values with no limits on data types that can be stored but now im trying to port a simple app i create in python to c++ that depended heavily on dictionaries.
so im asking for a little guidance here

#python code i want to recreate in c++

sTo = {"mile":1,"Nautical mile":0.868423,"Nautical League":0.289474,"Furlong":8,"Chain":80,"rod":320,"yard":1760,"Feet":5280,"inch":63360,
       "Km":1.609344,"Hm":16.09344,"Meter":1609.344,"Dm":16093.44,"Cm":160934.4,"Mm":1609344,"Light Second":0.00000536819

In C++ they're called maps. A C++ map is one of the standard containers. The C++ FAQ contains this about objects of different types in a container: http://www.parashift.com/c++-faq/heterogeneous-list.html
A common approach is to use the boost any library.

That said, your example code above doesn't actually store different types. It looks like it can be done with just double types.

#include <map>
#include <iostream>

int main()

std::map<std::string, double> aNiceMap;
aNiceMap.insert ( std::pair<std::string,double>("mile",1));
aNiceMap.insert ( std::pair<std::string,double>("Dm",16093.44));

std::cout << aNiceMap.find("Dm")->second << '\n';


thanks but how would i use map<string, int>::iterator

to loop through all the keys and display there corresponding values.cause i tried the following but kept getting an error

for (map<string, double> itr = milesto.begin(); itr != milesto.end(); itr++){

    cout <<itr->first;

but kept getting an error

It's never a bad idea to post the error.

To display it should look like this

std::map<std::string, double> milesto;

// load milesto

for(std::map<std::string, double>::iterator itr = milesto.begin(); itr != milesto.end(); ++itr)
    std::cout << itr->first << "\t" << itr->second << std::endl;

Also notice how I used ++itr instead of itr++. Using the preincrement operator will make the code slightly more efficent since it is not a built in type. Not a big deal but it is something to consider.

thanks you are a life saver!!!
at first i was wondering whats the difference then i realized that in my declaration for itr i left out ::iterator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.