簡體   English   中英

使用一個鍵和一對值遍歷 map

[英]Iterating through a map with one key and a pair of values

我有這個 map 並假設它已經充滿了數據。

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

假設它有鍵和一對看起來像這樣的值:

例如: "Hello", {10,20};

我已經知道如何一鍵一值 output。

std::map 一個鍵,兩個值這里顯示你可以有一個鍵和 2 個值,但我不知道如何遍歷它們並得到一個 output 就像在例子中一樣。

所以這對我有用。

我首先初始化了這些。

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

然后使用 for 循環。

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

對於 output。

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

我閱讀的資料來源:

遍歷pair的向量https://www.cplusplus.com/reference/unordered_map/unordered_map/insert/

請務必通知我。 也許我對這個不正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM