shane
shane

Reputation: 509

create multimap<string, vector<string> >

I have been given a vector< vector > and I have to put them in

std::multimap< string, vector<string> > subsetsMap;

with the first string of each tuple as key and the vector as value. Here is my function:

void hashThem()
{
    int i,j;
    vector<string> temp;
     string first;
    for(i=0;i<subset_list.size();i++)
    {   
        for(j=0;j<subset_list[i].size();j++)
            temp.push_back(subset_list[i][j]);
        first = temp[0];
        subsetsMap.insert(pair<first,temp>);
        temp.clear();
    }
}


The subset_list and subsetsMap are declared globally. The declaration of subset_list is:

vector< vector<string> > subset_list;

which has data like:
citrus fruit, margarine,
coffee, tropical fruit,
whole milk, tropical fruit,
cream cheese , meat spreads,
condensed milk, long life bakery product,
abrasive cleaner, butter, etc
But when compiling I am getting errors like:

dm1.cpp: In function ‘void hashThem()’: dm1.cpp:124:26: error: the value of ‘first’ is not usable in a constant expression
subsetsMap.insert(pair); ^ dm1.cpp:118:10: note: ‘first’ was not declared ‘constexpr’ string first; ^ dm1.cpp:124:32: error: the value of ‘temp’ is not usable in a constant expression subsetsMap.insert(pair); ^ dm1.cpp:117:17: note: ‘temp’ was not declared ‘constexpr’ vector temp; ^ dm1.cpp:124:36: error: type/value mismatch at argument 1 in template parameter list for ‘template struct std::pair’ subsetsMap.insert(pair); ^ dm1.cpp:124:36: error: expected a type, got ‘first’ dm1.cpp:124:36: error: type/value mismatch at argument 2 in template parameter list for ‘template struct std::pair’ dm1.cpp:124:36: error: expected a type, got ‘temp’

There is something wrong I am doing, but since I don't know much c++, and could not find any relevant google result, any help is appreciated. TIA

Upvotes: 1

Views: 1026

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 30489

subsetsMap.insert(pair<first,temp>); should be:

subsetsMap.insert(make_pair(first,temp));

std::make_pair is used to make a pair.

Upvotes: 1

Humam Helfawi
Humam Helfawi

Reputation: 20264

subsetsMap.insert(pair<first,temp>); is wrong.

It should be :

subsetsMap.insert(std::make_pair(first,temp));

Or:

subsetsMap.insert(std::pair<const std::string, std::vector<std::string>>(first,temp));

Notice the const for the key.


Even better:

subsetsMap.emplace(first,temp); // Forwarding the arguments directly to the constructor

Upvotes: 3

Related Questions