C++ compilation issue when trying to insert into a

2019-08-07 10:00发布

问题:

C++ compiler error that i am getting is :

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.

Below is my program:

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

The above code is throwing a compilation error when i try to insert a vector as value with a string as a key.I am working on solaris.

回答1:

Right way to insert element in map is:

mos[a] = v1

(due to map has overloaded operator[])



标签: c++ map vector