How to access Sets of pairs in c++

Viewed 83
#include<bits/stdc++.h>
using namespace std;

#define ll long long 

int main(){
    set<pair<ll,ll>>s;
    s.insert(make_pair(5,6));
    s.insert(make_pair(5,4));
    s.insert(make_pair(3,7));
    s.insert(make_pair(6,8));
    auto it=s.upper_bound(make_pair(4,0));
   
    cout<<*it.first<<endl;
}

It's printing:

error: ‘struct std::_Rb_tree_const_iterator >’ has no member named ‘first’

can someone answer me why it is happening and how to solve the error .

2 Answers

You can get element of pair with :

 cout<<std::get<0>(*it)<<endl;

In this expression:

*it.first

the operator . has higher precedence than *. So the expression becomes:

*(it.first)

which is not correct.

You need to disambiguate with parentheses, like this:

(*it).first

or even better, use the -> operator like this:

it->first
Related