Introduction
Set is a data structure that only stores unique elements
Initialize
#include <set>
using namespace std;
int main(){
set<int> iSet;
set<string> sSet;
iSet.insert(10);
}
Check for an element
if (iSet.find(20) != iSet.end()) {
cout << "20 is in the set" << endl;
}
Erase an element
iSet.erase(20);
Iterate over an element
for (auto it = iSet.begin(); it != iSet.end(); ++it) {
std::cout << *it << " ";
}
Get the size
std::cout << "Size: " << iSet.size() << std::endl;
Clear the set
iSet.clear();
Application
- Removing duplicates in an array
- Set Operations
SOCIAL SHARE CARD GENERATOR