24

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);
0

7 Answers 7

42

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

Sign up to request clarification or add additional context in comments.

3 Comments

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: Perhaps you have another class whose name is string that somehow collides with std::string ?
I have tested this on the latest VS 2012 version and it does the same thing. It's very confusing and peculiar that it doesn't complain about string not being defined.
10

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.

1 Comment

Oh, interesting! Is the compiler she? Allright, I can agree on that. :) - Just like our Mother Nature and Milkyway Galaxy (in Sanskrit: Shi-shu-maa-ra = She-dolphin).
7

Here is the way to set up map<...,...>

static std::map<std::string, RequestTypes> requestTypesMap = {
   { "order",       RequestTypes::ORDER       },
   { "subscribe",   RequestTypes::SUBSCRIBE   },
   { "unsubscribe", RequestTypes::UNSUBSCRIBE }
};

Comments

5

Try m[s1] = v1; instead.

1 Comment

And this gives you no way to tell if you inserted a new node or updated a pre-existing one.
4

You have several possibilities how store strings in key value format now:

m["key1"] = "val1";
m.insert(pair<string,string>("key2", "val2"));
m.insert({"key3", "val3"}); // c++11

And traverse it in c++11:

for( auto it = m.begin(); it != m.end(); ++it )
{
  cout << it->first; // key
  string& value = it->second;
  cout << ":" << value << endl;
}

1 Comment

m.insert({"key3", "val3"}); // c++11 helped me
1

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.

Comments

-1

The s1 is an integer you are hoping pass as string...thats likely the main cause of the error!!

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.