dev-resources.site
for different kinds of informations.
Set STL in CPP π¨βπ
Published at
11/22/2024
Categories
cpp
stl
datastructures
algorithms
Author
aryan015
Main Article
Introduction
Set is a data structure that only stores unique elements.
Types of Set
There are two types of set in cpp.
- Ordered Set - Maintain sorting while insertion
- Unordered Set - Insert in random order which makes it fast.
Initialize
#include <set>
using namespace std;
int main(){
set<int> iSet;
unordered_set<int> uSet;
iSet.insert(10);
uSet.insert(10);
}
Check for an element
if (iSet.find(20) != iSet.end()) {
cout << "20 is in the set" << endl;
}
if (uSet.find(20) != uSet.end()) {
cout << "20 is in the set" << endl;
}
Erase an element
iSet.erase(20);
uSet.erase(20);
Iterate over an element
Iteration is same in ordered and Unordered Set
for (auto it = iSet.begin(); it != iSet.end(); ++it) {
std::cout << *it << " ";
}
Get the size
std::cout << "Size: " << iSet.size() << std::endl;
std::cout << "Unordered Size: " << uSet.size() << std::endl;
Clear the set
iSet.clear();
Difference Between Ordered and Unordered set
Feature | std::set |
std::unordered_set |
---|---|---|
Order | Stores elements in sorted order | No particular order |
Time Complexity | O(log n) for insert, delete, search | O(1) average for insert, delete, search |
Duplicates | Not allowed | Not allowed |
Use Case | When sorting is needed | When fast operations are needed |
Application
- Removing duplicates in an array
- Set Operations
Articles
12 articles in total
Set STL in CPP π¨βπ
currently reading
Media Attribute in HTML
read article
LocalStorage VS SessionStorage VS Cookie
read article
Day 7: Create a calculator in Python [exercise]
read article
Symbols and Objects in JSβ₯
read article
πunderstanding windows Command Line Interface
read article
Useful npm commands π©βπ»
read article
async-await and promises explained in JS [simplified]
read article
π§‘how to set precision in cpp for floating or double datatype πͺ
read article
π§‘How to take input in javascript using console (codechef)
read article
day 6: comments and escape sequence and flushes in python πββοΈ
read article
Day 5: Writing our first program in python with homeπ§‘work
read article
Featured ones: