Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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])++;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading