Set lower/upper bound implementation

Hello, I have some troubles with this code, which I can't compile/run...

It's all about upper bound with set (with your own structure).
Some help'd be appreciated ! :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>
#include <set>

using namespace std;

struct pos{
	int x,y;
};

bool operator<(pos a,pos b){  
    return a.x < b.x;  
}

set <pos> S;
set <pos> ::iterator l;

int main(){
	pos A;
	A.x = 2;
	A.y = 6;
	S.insert(A);
	
	
	A.x = 5;
	A.y = 2;
	S.insert(A);
	
	
	A.x = 3;
	A.y = 4;
	S.insert(A);
	
	
	l = S.lower_bound(5);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <set>

struct pos {

    int x,y;
};

bool operator< ( pos a, pos b ) {

    if( a.x == b.x ) return a.y < b.y ;
    else return a.x < b.x;
}

int main() {

    std::set<pos> s = { {2,6}, {5,2}, {3,4}, {6,1}, {5,0}, {4,9} } ;
    
    // auto iter = s.lower_bound(4) ;
    auto iter = s.lower_bound( pos{5,0} ); // value_type of the set is pos
    std::cout << iter->x << ' ' << iter->y << '\n' ; // 5 0

    iter = s.upper_bound( pos{5,0} );
    std::cout << iter->x << ' ' << iter->y << '\n' ; // 5 2
}

http://coliru.stacked-crooked.com/a/13edf8753014b37b
Topic archived. No new replies allowed.