mardi 31 mai 2022

C++ get and set item in map as value inside unordered_map?

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.

Aucun commentaire:

Enregistrer un commentaire