Home > Blockchain >  C get and set item in map as value inside unordered_map?
C get and set item in map as value inside unordered_map?

Time:06-01

std::unordered_map<string, tuple<int, vector<int>>> combos;

I want to retrieve items inside tuple and also to increase or set their value. e.g.

if(v_intersection.size()==5){
                string stringval = join(v_intersection, ",");
                if (combos.find(stringval) == combos.end()) // if key is NOT present already
                {
                    combos[stringval]get<0>(combos[stringval]) = 1; // initialize the key with value 1
                }
                else
                {
                    combos[stringval]  ; // key is already present, increment the value by 1
                }
            }

I want to set int (the first parameter of tuple) to 1 and also increase it by 1 when needed.

It is not inside a for loop but in a if conditional clause.

How to do that?

I tried combos[stringval]get<0>(combos[stringval]) = 1; but it is not working.

What if I want to add items inside the vector from that map? How to do it?

Also, if you have any idea on how to do this using something else other than nested maps with tuples, please give to me your advise.

Thank you in advance!

It is not duplicate, please check it out. There are some topics but asking for something else not nested maps with tuples as values.

CodePudding user response:

std::get(std::tuple) is a free function that takes a tuple as parameter. So you have to call it like so: get<INDEX>(some_tuple).

Knowing this, answering your question is just a matter of passing the tuple from the map to that function.

I want to set int (the first parameter of tuple) to 1

std::get<0>(combos[stringval]) = 1;

and also increase it by 1 when needed.

std::get<0>(combos[stringval])  ;
  • Related