0

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

3
  • 2
    String in C++ use double quotes, have you tried changing your simple quotes to those? Commented Jun 20, 2018 at 14:10
  • 2
    C++ doesn't use ' and " interchangeably. They are use to define different types of literals. Commented Jun 20, 2018 at 14:11
  • thank you both. that sorted my issue. Commented Jun 20, 2018 at 14:12

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.