如何将char *插入map <string,string>?</string,string>

时间:2012-11-05 00:49:50

标签: c++ string map char

我有2个单词被视为char*,我想将它们插入map<string,string>,因此我尝试了两种方式而且它们不起作用 -

第一路 -

#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) ; 
}

我收到了错误 -

/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> > > >]

第二路 -

    // 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) ; 

我收到了错误 -

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’

3 个答案:

答案 0 :(得分:5)

documentation 1 可以看出,您需要的map.insert重载只需要一个参数。在那里它不是立即明显的,但它需要一个pair<string, string>,包含键和值。你可以这样做,例如:

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

可以动态地将char[]转换为string“,使用它们创建pair并将其插入地图中;您无法执行make_pair(cWord, cValue),因为没有从pair<char *, char *>pair<string, string>的隐式转换;

但是,在这种情况下,使用operator[] 2 更容易:

myMap[cWord]=cValue;

请注意,这里可以避免构造string临时值,因为在此上下文中,编译器可以自动创建它们。

顺便说一下,它是int main,而不是void main


  1. Here更准确,更新了C ++ 11,但恕我直言新手不太清楚。

  2. 但请记住,当密钥已存在时,insertoperator[]的语义会有所不同:operator[]会覆盖,而insert则不会执行任何操作。< / p>

答案 1 :(得分:1)

像这样:

myMap[cWord] = cValue;

或者,使用一对:

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

emplace

myMap.emplace(cWord, cValue);

如果要无条件地设置映射值,则应使用[] - 访问者;如果要控制密钥已存在的情况,则应使用emplace / insert函数

答案 2 :(得分:1)

#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;
}

整流:

#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; 
}