简体   繁体   中英

Iterating through a map with one key and a pair of values

I have this map and assumed that it's already filled with datas.

map<string,pair<int,int> >mymap;

Lets say it has key and a pair of values that looks like this:

ex: "Hello", {10,20};

I already know how to output with one key and one value.

std::map one key, two values it's shown here that you can have one key with 2 values but i don't know how to iterate through them and get an output like in the example.

So this worked for me.

I first i initialized these.

pair<int,int> pairs;
map<string, pair<int,int> > mymap;

then using a for loop.

pairs.first = //some integer;
pairs.second = //some integer; 
mymap[ //here i used the values from an array so i used array[current_index] ] = pairs;

for the output.

for(auto i : mymap) {
    //i.first for the key
    //i.second.first and i.second.second for the pair values
}

sources that i read:

Iterate over vector of pair , https://www.cplusplus.com/reference/unordered_map/unordered_map/insert/

Please do notify me. Maybe i'm incorrect with this one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM