URL87
URL87

Reputation: 11012

How to insert char* into map<string,string>?

I have 2 words that taken as char* , I want insert them to map<string,string> , thus I tried 2 ways and they didn't work -

1st way -

#include <iostream>
#include <string>
#include <map>
using namespace std;
void main()
{
    map<string,string> myMap ; 
    char cWord[4] = "abc"; 
    char cValue[4] = "123";
    myMap.insert(cWord,cValue) ; 
}

I got an error -

/usr/include/c++/4.4/bits/stl_tree.h: In member function ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = char*, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:
/usr/include/c++/4.4/bits/stl_map.h:553:   instantiated from ‘void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = char*, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’
testString.cpp:10:   instantiated from here
/usr/include/c++/4.4/bits/stl_tree.h:1324: error: no matching function for call to ‘std::_Rb_tree<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::_M_insert_unique_(std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, char&)’
/usr/include/c++/4.4/bits/stl_tree.h:1206: note: candidates are: std::_Rb_tree_iterator<_Val> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique_(std::_Rb_tree_const_iterator<_Val>, const _Val&) [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]

2nd way -

    // same include's as above ..
    map<string,string> myMap ; 
    // char* init ..    
    char cWord[4] = "abc"; 
    char cValue[4] = "123";

    // string ..
    string word(cWord) ; 
    string value(cValue) ; 
    myMap.insert(word,value) ; 

I got an error -

error: ‘::main’ must return ‘int’
In file included from /usr/include/c++/4.4/map:60,
                 from testString2.cpp:3:
/usr/include/c++/4.4/bits/stl_tree.h: In member function ‘void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(_II, _II) [with _InputIterator = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Val = std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _KeyOfValue = std::_Select1st<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:
/usr/include/c++/4.4/bits/stl_map.h:553:   instantiated from ‘void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’
testString2.cpp:15:   instantiated from here
/usr/include/c++/4.4/bits/stl_tree.h:1323: error: no match for ‘operator++’ in ‘++__first’
/usr/include/c++/4.4/bits/stl_map.h:553:   instantiated from ‘void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’
testString2.cpp:15:   instantiated from here
/usr/include/c++/4.4/bits/stl_tree.h:1324: error: no match for ‘operator*’ in ‘*__first’

Upvotes: 0

Views: 6473

Answers (3)

Matteo Italia
Matteo Italia

Reputation: 126787

As you can see from the documentation1, the map.insert overload that you need takes a single parameter. There it's not immediately evident, but it wants a pair<string, string>, containing the key and the value. You can do, for example:

myMap.insert(make_pair(string(cWord),string(cValue))); 

which converts the char[] to string "on the fly", creates the pair with them and inserts it in the map; you cannot do make_pair(cWord, cValue) because there's not an implicit conversion from pair<char *, char *> to pair<string, string>;

Still, in this case it's way easier to just use the operator[]2:

myMap[cWord]=cValue;

Notice that here you can avoid the construction of the string temporaries, because in this context the compiler is allowed to create them automatically.

By the way, it's int main, not void main.


  1. Here is more precise and up to date with the C++11 additions, but IMHO less clear for a novice.

  2. But keep in mind that the semantic of insert and operator[] is different when the key already exists: operator[] overwrites, while insert does nothing.

Upvotes: 5

Kerrek SB
Kerrek SB

Reputation: 476990

Like so:

myMap[cWord] = cValue;

Alternatively, with a pair:

myMap.insert(std::pair<std::string const, std::string>(cWord, cValue));

Or with emplace:

myMap.emplace(cWord, cValue);

You should use the []-accessor if you want to set the mapped value unconditionally, and the emplace/insert functions if you want control over the situation where the key already exists.

Upvotes: 1

Aniket Inge
Aniket Inge

Reputation: 25695

#include <iostream>
#include <string>
#include <map>
using namespace std;
void main() //first mistake, it should be int main()
{
    map<string,string> myMap ; 
    char cWord[4] = "abc"; 
    char cValue[4] = "123";
    myMap.insert(cWord,cValue) ; //third mistake, char * != string.
    //and return 0;
}

Rectification:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
    map<string,string> myMap ; 
    string cWord = "abc", cValue = "123";
    myMap[cWord] = cValue ;
    return 0; 
}

Upvotes: 1

Related Questions