0

として 2 つの単語がありchar*、それらを に挿入したいmap<string,string>ので、2 つの方法を試しましたが、うまくいきませんでした -

第 1 の方法 -

#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

ドキュメント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. 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 に答える