我正在使用里面的地图, 但无法弄清楚如何使用地图的迭代器访问
我的地图声明
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>'|
答案 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];