tags:

views:

128

answers:

4

Hi, 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);
A: 

Try m[s1] = v1; instead.

tdammers
This too gives the same error.
bjskishore123
And this gives you no way to tell if you inserted a new node or updated a pre-existing one.
John Dibling
+8  A: 

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

Etienne de Martel
This made the code compiled. But am wondering why it didn't give error when i said string s1 = "1"; Only upon calling insert it gives error.
bjskishore123
@bjskishore123: Perhaps you have another class whose name is `string` that somehow collides with `std::string` ?
ereOn
@ereOn - or maybe that's not the only compiler error
Steve Townsend
+1  A: 

Could you try this:

#include<string>

It seems the compiler doesn't know how to compare strings. Maybe she doesn't know enough about strings yet, but is too focused on your map to figure that out ATM.

Daren Thomas
A: 

I think it has to do with the fact that <map> doesn't include <string>, but <xstring>. When you are adding elements to the map it needs to find the correct position in the map by sorting. When sorting, map tries to locate the operator <, from which it finds the correct location for the new element. However, there is no operator < for the definition of string in <xstring>, thus you get the error message.

Default