1
予定義的映射等如何遍歷地圖上的形式對<整型,對<int,int>>的與迭代
map <int,pair<int,int>> hmap;
如果有一個pair(2,pair(3,4))
如何獲得2個3 4值,itr->first
,itr->second
不工作
予定義的映射等如何遍歷地圖上的形式對<整型,對<int,int>>的與迭代
map <int,pair<int,int>> hmap;
如果有一個pair(2,pair(3,4))
如何獲得2個3 4值,itr->first
,itr->second
不工作
這裏是一個示範項目使用迭代器和基於範圍的語句。
#include <iostream>
#include <map>
int main()
{
std::map<int, std::pair<int, int>> hmap{ { 1, { 2, 3 } }, { 2, { 3, 4 } } };
for (auto it = hmap.begin(); it != hmap.end(); ++it)
{
std::cout << "{ " << it->first
<< ", { " << it->second.first
<< ", " << it->second.second
<< " } }\n";
}
std::cout << std::endl;
for (const auto &p : hmap)
{
std::cout << "{ " << p.first
<< ", { " << p.second.first
<< ", " << p.second.second
<< " } }\n";
}
std::cout << std::endl;
}
它的輸出是
{ 1, { 2, 3 } }
{ 2, { 3, 4 } }
{ 1, { 2, 3 } }
{ 2, { 3, 4 } }
如果有一個
pair(2,pair(3,4))
如何獲得2個3 4值[從一個迭代itr
到map<int,pair<int, int>>
]
我想
itr->first // 2
itr->second.first // 3
itr->second.second // 4
這樣的語法,甚至不存在C,請不要標記語言無關! –