Skip to content

Instantly share code, notes, and snippets.

@uhmseohun
Created April 12, 2020 04:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uhmseohun/c4738791fd3f2c23acdca20f0510b235 to your computer and use it in GitHub Desktop.
Save uhmseohun/c4738791fd3f2c23acdca20f0510b235 to your computer and use it in GitHub Desktop.
STL SET
#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