1

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

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

9 Comments

The 2nd one (the one with const worked). The other two (i.e. the make_pair and emplace one didnt work). Thanks..
@Shane I would love to see the day, when people stopped using such vague (you cannot get more vague) statements as didn't work..
@shane Welcome... what is your compiler?
@shane std::make_pair and std::multi_map::emplace are C++11 things. Does your compiler support C++11? It seems not. Since std::multi_map and std::pair wroks but both emplace and make pair not. It is not C++11 Compiler.. Consider moving to some compiler with at least C++11 or even C++14
@AlgirdasPreidžius :) I like them sometimes because they triggers my prediction system. However, I always make false predictions :D
|
1

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

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

std::make_pair is used to make a pair.

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.