C++ – map how to insert data in this map

cstdmapstdstringstl

I need to store strings in key value format. So am using Map like below.

#include<map>
using namespace std;
int main()
{
    map<string, string> m;
    string s1 = "1";
    string v1 = "A";

    m.insert(pair<string, string>(s1, v1)); //Error
}

Am getting below error at insert line

error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

I tried make_pair function also like below, but that too reports the same error.

m.insert(make_pair(s1, v1));

Pls let me know what's wrong and what's the solution for above problem.
After solving above problem, can i use like below to retrieve value based on key

m.find(s1);

Best Answer

I think you miss a #include <string> somewhere.

Related Topic