2016-05-06 10 views

答えて

0

あなたがstd::map<X, Y>の要素を指すイテレータitを持っているなら、あなたはit->firstを使用してキーとit->secondを使用してマップされた型への参照をconst参照を取得することができ、itポイント型であるstd::map<X, Y>::value_type値にあるため例えば

std::pair<std::map<X, Y>::key_type const, 
      std::map<X, Y>::mapped_type> 

Y * setX(std::map<X, X> & dict, X const & value) { 
    std::map<X, Y>::iterator it = dict.find(value); 
    if (it != dict.end()) 
     return nullptr; 
    assert(it->first == value); 
    return &it->second; 
} 
0
#include <map> 
#include <iostream> 
#include <string> 
using namespace std; 


int main() 
{ 
    // for a simple struct you could use "pair" (especially if you don't want to name it). 
    // anyway: 
    map<int, pair<double, string>> mymap; 
    mymap[0] = pair<double, string>(4.56, "hello"); 
    mymap[1] = pair<double, string>(9.87, "hi"); 
    for(auto & item : mymap) 
    { 
     cout << "Key: " << item.first << ", Value: [" << item.second.first << ", " << item.second.second << "]" << endl; 
    } 
    return 0; 
} 

出力:

キー:0、値[4.56、ハロー]

キー:1、値[9.87、HI]

関連する問題