#include <iostream>
#include <set>
int main()
{
std::set<int> orderedSet;
orderedSet.insert(1);
orderedSet.insert(2);
for (auto i : orderedSet) {
std::cout << i << "\n";
}
// Re-initialise the set
orderedSet = {8, 8, 7, 0};
std::cout << "\n";
for (auto i : orderedSet) {
std::cout << i << std::endl;
}
// Search the element by the value
int searchEle = 6;
std::set<int>::iterator itr = orderedSet.find(searchEle);
if (itr != orderedSet.end()) {
int val = *itr;
std::cout << val << " is found\n";
} else {
std::cout << searchEle << " is not found\n";
}
// Search the element by the index - C++20
int searchInd = 0;
std::set<int>::iterator itr2 = orderedSet.begin();
std::advance(itr2, searchInd);
if (itr2 != orderedSet.end()) {
int val = *itr2;
std::cout << val << " is found at Index " << searchInd << "\n";
} else {
std::cout << searchEle << " is not found at Index " << searchInd << "\n";
}
// Search the element by the index - Old school Method
int searchInd2 = 2;
int val = -1, count = 0;
std::set<int>::iterator itr3 = orderedSet.begin();
while (itr3 != orderedSet.end()) {
if (searchInd2 == count) {
val = *itr3;
std::cout << val << " is found at Index " << searchInd2 << "\n";
break;
}
count++;
itr3++;
}
if (val == -1) {
std::cout << searchEle << " is not found at Index " << searchInd2 << "\n";
}
return 0;
}