0

I am trying to insert a value in map where key to map is string and value is list. When I try to insert then I am getting error.

#include <iostream>
#include <utility>
#include <vector>
#include <map>
#include <string>
using namespace std;
main()
{
     string key = "myKey";
     string str1 = "str1";

     map<string, list<string>> myMap;
     myMap.insert( make_pair (key, str1));

 }

error

Error 2 error C2664: 'std::pair<_Ty1,_Ty2> std::_Tree<_Traits>::insert(std::pair &&)' : cannot convert parameter 1 from 'std::pair<_Ty1,_Ty2>' to 'std::pair<_Ty1,_Ty2> &&'

Help is appreciated !!

Mehul Donga
  • 105
  • 3
  • 14

1 Answers1

3

You have a std::map that takes a key which is a string and a list as value. You are trying to pass it a key that is a string and a string as value which is the problem.

main()
{
     string key = "myKey";
     string str1 = "str1";
     list<string> l;

     l.push_back( str1 );

     map<string, list<string>> myMap;
     myMap.insert( make_pair (key, l)); // pass a list here

    return 0;
}
deW1
  • 5,562
  • 10
  • 38
  • 54