3
#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 .

bruno
  • 31,755
  • 7
  • 21
  • 36
Bala vamsi
  • 152
  • 6
  • 8
    `.` has higher precedence than `*` so you need parens, `cout << (*it).first << endl;` or more simply `cout << it->first << endl;` – john Jul 05 '20 at 08:40
  • 1
    operator precedence, you want to know it – Swift - Friday Pie Jul 05 '20 at 08:42
  • 6
    also, don't use [#include](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – yaodav Jul 05 '20 at 08:43
  • Can i Know the reason @yaodav – Bala vamsi Jul 05 '20 at 08:55
  • read to links, but basically #include is an internal header for the compiler and it includes a lot of irrelevant data and using namespace std can cause function ambiguity. if in the std you have a function that has the same name as the one you define and the compiler don't know wich one to use or he is using the wrong one – yaodav Jul 05 '20 at 08:59

2 Answers2

2

You can get element of pair with :

 cout<<std::get<0>(*it)<<endl;
Farhad Sarvari
  • 1,021
  • 6
  • 13
1

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
cigien
  • 50,328
  • 7
  • 37
  • 78