2013-06-28 43 views
1

C++编译器错误时,C++编译的问题是:试图插入一个地图,我得到

line 27: Error: Could not find a match for 
    std::multimap<std::string, std::vector<std::string>, 
        std::less<std::string>, 
        std::allocator<std::pair<const std::string, 
         std::vector<std::string>>>> 
    ::insert(std::pair<std::string, std::vector<std::string>>) 
    needed in main(). 
1 Error(s) detected. 

下面是我的程序:

#include<iostream> 
#include<sstream> 
#include<map> 
#include<vector> 
#include<algorithm> 
using namespace std; 

typedef multimap<string, vector<string> > mos_map; 
typedef multimap<string, vector<string> >::iterator mos_map_it; 

int main() 
{ 

    mos_map mos; 
    mos_map_it it; 

    vector<string> v1; 

    v1.push_back("a"); 
    v1.push_back("b"); 
    v1.push_back("c"); 
    v1.push_back("mo1"); 

    std::string a(*(v1.end()-1)); 

    mos.insert(std::pair< std::string, vector<std::string> >(a,v1)); 
    //Is the above not the right way to to insert an element into the map? 
    return 0; 
} 

上面的代码抛出一个编译错误时我尝试用一​​个字符串作为key来插入一个向量作为值。我在solaris上工作。

+4

你忘了'的#include '。 –

+0

即使添加了#include ,我也得到了同样的错误。 – user1939168

+1

你使用的是什么编译器? –

回答

-1

在地图中插入元件正确的做法是:

mos[a] = v1 

(由于地图已重载的运算符[])

+0

这是方便的方法。这也是效率较低的方法:如果地图中还没有“a”,则首先创建一个默认值,然后将“v1”分配给新的地图条目。 'insert'不是这种情况。 – xtofl

+0

这是我使用的第一件事,它不起作用。 – user1939168

+0

这工作:'mos.insert(mos_map :: value_type(a,v1));' – user1939168

相关问题