Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

map<string, string> how to insert data in this map?

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);
like image 946
bjskishore123 Avatar asked Sep 14 '10 12:09

bjskishore123


People also ask

How do you add a string value to a string map?

To insert the data in the map insert() function in the map is used. It is used to insert elements with a particular key in the map container. Parameters: It accepts a pair that consists of a key and element which is to be inserted into the map container but it only inserts the unique key.

How do you add data to a map?

You can use the Add Data button on the ArcMap toolbar to add data to your map. Click Add Data, browse to the data you want to add, then click Add. The data is added to the table of contents, but you'll still have to geocode the data's attribute table on the map to make it available for customer or store setup.

How do you add a value to a map on a map?

The standard solution to add values to a map is using the put() method, which associates the specified value with the specified key in the map. Note that if the map already contains a mapping corresponding to the specified key, the old value will be replaced by the specified value.

Can we use string in map in C++?

A map can be declared as follows: #include <iostream> #include <map> map<int, int> sample_map; Each map entry consists of a pair: a key and a value. In this case, both the key and the value are defined as integers, but you can use other types as well: strings, vectors, types you define yourself, and more.


1 Answers

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

like image 128
Etienne de Martel Avatar answered Oct 01 '22 17:10

Etienne de Martel