Reputation: 29
I am using the map with pair inside it, but not able to figure out how to access with iterator of map
my map declaration
map<ll,pair<ll,ll> > val;
map<ll,pair<ll,ll> > ::iterator it;
What i am using for accessing inserted values is
cout<<it->first<<" " <<it->second->first<<" " <<it->second->second<<endl;
But the compiler is showing this error
error: base operand of '->' has non-pointer type 'std::pair<long long unsigned int, long long unsigned int>'|
Upvotes: 2
Views: 5806
Reputation: 49
You can also return pair values together.
map<ll,pair<ll,ll> > val;
auto [pair_first_element,pair_second_element]=val[key_value];
Upvotes: 1
Reputation: 885
Use ->
to access the element pointed to by a pointer and a .
to access a member variable. In this case, map
is a container and pair
a struct so you have to access the elements of both with .
.
cout << it->first << " " << it->second.first << " " << it->second.second << endl;
Upvotes: 1
Reputation: 362187
Use .
to access the elements of a pair.
cout<<it->first<<" " <<it->second.first<<" " <<it->second.second<<endl;
Upvotes: 3