user3867743
user3867743

Reputation: 87

How to use a type array in map in c++

I want to have something like dictionary and because of this matter I used map. I want the "key" be an int and "value" to be an array. To reach this, I did the following but I can not go further,

typedef std::string RelArr[2];
std::map<int,RelArr> mymap;

Problem I can not assign anything to "value"

mymap.insert(myintnumber,"ValueOfFirstIndex","ValueOfSecondIndex");

I receive this error:

Multiple markers at this line
    - deduced conflicting types for parameter ‘_InputIterator’ (‘int’ and ‘const char*’)
    - candidates are:
    - no matching function for call to ‘std::map<int, std::basic_string<char> [2]>::insert(int, const char [3], const char 
     [19])’

Upvotes: 1

Views: 4314

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409176

There are two problems here, the first is that arrays can not be assigned to, only copied to, which makes them unusable to put in containers. Use std::array instead:

typedef std::array<std::string, 2> RelArr;

The second problem is regarding what the error actually tells you is that you're using the std::map::insert function wrong, because there's no overload which takes two string literals.

If you want to insert an element in a std::map just do e.g.

mymap[myintnumber] = {{ "string1", "string2" }};

Or if you desperately want to use insert:

auto ret = mymap.insert(std::make_pair<int, RelArr>(
                            myothernumber, {{"hello", "world"}}));
if (ret.second == true)
    std::cout << "Insertion was okay\n";
else
    std::cout << "Insertion failed\n";

Upvotes: 2

Related Questions