Skip to content

Commit ad61f56

Browse files
authored
Merge pull request #3 from adityyyy/adityyyy-patch-3
Create seIterators.cpp
2 parents f627444 + 55b40de commit ad61f56

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

CPP/set & pair/seIterators.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
5+
//Set iterators offer less features than vectorr iterators.
6+
//auto it = s.begin(); //it is the iterator to the first element
7+
// it++, it--, ++it, --it ->These are all valid and wor in O(log N) time
8+
9+
//NOTE: (it+5) or it+=2 etc are invalid. to advance multiple steps, do it++ multiple times.
10+
11+
//elements of iteratir are always in sorted manner
12+
set<int> s;
13+
s.insert(4);
14+
s.insert(1);
15+
s.insert(10);
16+
s.insert(2);
17+
18+
cout<<*s.begin()<<endl;
19+
20+
s.erase(s.begin());
21+
cout<<*s.begin()<<endl;
22+
23+
/* FUNCTIONS RELATED TO SET ITERATORS:
24+
s.find(x): returns iterator to element with value x. Returns s.end() if not found. O(log N)
25+
26+
s.lower_bound(x): returns iterator to the first element which is >=x. Returns s.end() if not found. O(log N)
27+
28+
s.upperbound(x): returns iterator to the first element which is >x. Returns s.end() if not found. O(log N)
29+
30+
s.erase(it): erases the element with iterator it. O(log N)
31+
32+
following 2 lines are same
33+
it(s.find(10)==s.end()) cout<<"Not found";
34+
it(s.count(10)==0) cout<<"Not found";
35+
*/
36+
return 0;
37+
}

0 commit comments

Comments
 (0)