2017-10-20 3 views

Antwort

1

hier ein Demonstrationsprogramm ist Iteratoren und die bereichsbasierte für Aussage bei der Verwendung.

#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; 
} 

Sein Ausgang ist

{ 1, { 2, 3 } } 
{ 2, { 3, 4 } } 

{ 1, { 2, 3 } } 
{ 2, { 3, 4 } } 
2

Wenn es eine pair(2,pair(3,4)) wie 2 3 4 Werte erhalten [von einem Iterator itr-map<int,pair<int, int>>]

Ich nehme an

itr->first   // 2 
itr->second.first // 3 
itr->second.second // 4 
Verwandte Themen