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 !!

2
  • Note std::map has been introduced prior to C++11 Commented Dec 30, 2015 at 9:37
  • Why not use multimap or unordered_multimap? Commented Dec 30, 2015 at 12:28

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.