简体   繁体   中英

Python dictionary to C++ map

I am very new to C++ and am trying to translate a dictionary into a C++ format. I can't quite seem to find the answer I am looking for from the previous questions submitted on here.

I have code as follows:

#include <iostream>
#include <map>

using namespace std;

typedef std::map<string, int> BasePairMap;

int main()
{
    BasePairMap m;

    m['power'] = 0;
    m['select'] = 1;
    m['backup'] = 2;
    ...
    ...
    ...
    m['rewind'] = 71;
    m['boxoffice'] = 240;
    m['sky'] = 241;


    return 0;
}

But I keep getting character overflow errors. How can I map string/int pairs together in C++?

Thanks

While many languages (such as Python) allow developers to use either single or double quotes for strings, in C++ you need to use double quotes ( reference ). Simple quotes are used for the char type which describes a single character ( reference ).

So your code should be:

#include <iostream>
#include <map>

using namespace std;

typedef std::map<string, int> BasePairMap;

int main()
{
    BasePairMap m;

    m["power"] = 0;
    m["select"] = 1;
    m["backup"] = 2;
    // ...
    m["rewind"] = 71;
    m["boxoffice"] = 240;
    m["sky"] = 241;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM