0

我有 2 个单词char*,我想将它们插入map<string,string>,因此我尝试了 2 种方法,但它们没有用 -

第一种方式——

#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’
4

3 回答 3

5

正如您从文档1map.insert中看到的,您需要的重载采用单个参数。那里不是很明显,但它需要一个pair<string, string>,包含键和值。例如,您可以这样做:

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

它将 转换char[]string“即时”,pair与它们一起创建并将其插入到地图中;你不能这样做make_pair(cWord, cValue),因为没有从pair<char *, char *>to的隐式转换pair<string, string>

operator[]不过,在这种情况下,使用2会更容易:

myMap[cWord]=cValue;

请注意,在这里您可以避免构建string临时文件,因为在这种情况下,编译器可以自动创建它们。

顺便说一句,它是int main,不是void main


  1. 是更精确和最新的 C++11 添加,但恕我直言,新手不太清楚。

  2. 但是请记住,当键已经存在时, insertand的语义是不同的:覆盖,而 while什么也不做。operator[]operator[]insert

于 2012-11-05T00:54:57.637 回答
1

像这样:

myMap[cWord] = cValue;

或者,使用一对:

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

或与emplace

myMap.emplace(cWord, cValue);

如果你想无条件地设置映射值,你应该使用[]-accessor,如果你想控制键已经存在的情况,你应该使用emplace/函数。insert

于 2012-11-05T00:53:42.303 回答
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; 
}
于 2012-11-05T00:54:18.450 回答