I defined map like
map <int,pair<int,int>> hmap;
If there is a pair(2,pair(3,4))
how to get 2 3 4 values, itr->first
, itr->second
not working
If there is a
pair(2,pair(3,4))
how to get 2 3 4 values [from an iteratoritr
tomap<int,pair<int, int>>
]
I suppose
itr->first // 2
itr->second.first // 3
itr->second.second // 4
Here is a demonstrative program with using iterators and the range-based for statement.
#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;
}
Its output is
{ 1, { 2, 3 } }
{ 2, { 3, 4 } }
{ 1, { 2, 3 } }
{ 2, { 3, 4 } }
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.