Created
April 12, 2020 04:55
-
-
Save uhmseohun/c4738791fd3f2c23acdca20f0510b235 to your computer and use it in GitHub Desktop.
STL SET
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <set> | |
#include <string> | |
using namespace std; | |
set <int> s; | |
void push () { | |
int val; | |
cin >> val; | |
if (s.insert(val).second) | |
cout << "pushed " << val << " to set\n"; | |
else | |
cout << val << " is already in set\n"; | |
} | |
void pop () { | |
int val; | |
cin >> val; | |
if (s.erase(val)) | |
cout << "removed " << val << " from set\n"; | |
else | |
cout << val << " is not in set\n"; | |
} | |
void search () { | |
int val; | |
cin >> val; | |
set <int>::iterator p; | |
p = s.find(val); | |
if (p != s.end()) | |
cout << val << " is located at index " << distance(s.begin(), p)+1 << "\n"; | |
else | |
cout << val << " is not in set\n"; | |
} | |
void output () { | |
set <int>::iterator p; | |
for (p=s.begin(); p!=s.end(); ++p) | |
cout << *p << " "; | |
cout << "\n"; | |
} | |
int main () { | |
string type; | |
while (true) { | |
cin >> type; | |
if (type == "insert") push(); | |
else if (type == "erase") pop(); | |
else if (type == "print") output(); | |
else if (type == "search") search(); | |
else if (type == "quit") break; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment