如何访问其中有一对内的地图

时间:2017-02-12 05:24:22

标签: c++ stdmap std-pair

我正在使用里面的地图, 但无法弄清楚如何使用地图的迭代器访问

我的地图声明

  map<ll,pair<ll,ll> > val;
  map<ll,pair<ll,ll> > ::iterator it;      

我用于访问插入值的是

   cout<<it->first<<" " <<it->second->first<<" " <<it->second->second<<endl;

但编译器显示此错误

error: base operand of '->' has non-pointer type 'std::pair<long long unsigned int, long long unsigned int>'|

3 个答案:

答案 0 :(得分:2)

使用.访问货币对的元素。

cout<<it->first<<" " <<it->second.first<<" " <<it->second.second<<endl;

答案 1 :(得分:0)

使用->访问指针指向的元素,使用.访问成员变量。在这种情况下,map是一个容器,pair是一个结构,因此您必须使用.访问两者的元素。

cout << it->first << " " << it->second.first << " " << it->second.second << endl;

答案 2 :(得分:0)

您也可以一起返回对值。

map<ll,pair<ll,ll> > val;

auto [pair_first_element,pair_second_element]=val[key_value];