C++17 में सेट (Set): विस्तृत मार्गदर्शिका
What is a Set?
Imagine a box for storing unique toys. If you try to put two identical balls in it, the second ball simply won't fit — the box already has one like it.
In C++, such a "box" for data is std::set. It is a collection that stores elements only once and automatically sorts them in ascending order.
Why is this needed? For example, to:
- Remove duplicates from a list (keep only unique items).
- Check if an element exists in the collection (if a contact is saved in the address book).
- Merge two lists without duplicates (a shopping list from two friends).
How to include and declare a set?
To use a set, you need to include the #include <set> library.
#include <iostream>
#include <set> // Include sets
#include <string>
int main() {
// Create a set of strings
std::set<std::string> ingredients;
// Create a set of integers
std::set<int> numbers;
return 0;
}
How to add an element?
Use the .insert() method. If the element already exists, it will not be added again.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
mySet.insert(10);
mySet.insert(5);
mySet.insert(10); // This element will not be added because 10 already exists!
mySet.insert(3);
// Print all elements (they will be sorted)
for(int num : mySet) {
std::cout << num << " ";
}
// Output: 3 5 10
return 0;
}
How to check if an element exists?
The .count() method returns 1 (if the element exists) or 0 (if it does not).
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> contacts = {"Anna", "Boris", "Vika"};
std::string name;
std::cout << "Enter a name to search for: ";
std::cin >> name;
if(contacts.count(name) == 1) {
std::cout << "Contact found!";
} else {
std::cout << "Contact not in the address book.";
}
return 0;
}
How to delete an element?
The .erase() method removes an element by value.
#include <iostream>
#include <set>
int main() {
std::set<std::string> shopping = {"milk", "bread", "eggs", "cheese"};
std::string item;
std::cout << "What do you want to remove from the list? ";
std::cin >> item;
shopping.erase(item); // Remove the element
std::cout << "Remaining in the list: ";
for(const auto& thing : shopping) {
std::cout << thing << " ";
}
return 0;
}
How to find the size of a set?
The .size() method returns the number of elements.
#include <iostream>
#include <set>
int main() {
std::set<int> numbers = {1, 2, 3, 4, 5, 5, 5}; // Duplicates are not counted
std::cout << "Set size: " << numbers.size(); // Output: 5
return 0;
}
Analysis of Typical Problems
Problem 1. Unique Pizza Ingredients
The user enters ingredients separated by commas. You need to output only the unique ones.
#include <iostream>
#include <set>
#include <sstream> // For working with strings
#include <string>
int main() {
std::string input;
std::cout << "Enter ingredients separated by commas: ";
std::getline(std::cin, input);
std::set<std::string> uniqueIngredients;
std::stringstream ss(input);
std::string ingredient;
// Split the string by commas
while(std::getline(ss, ingredient, ',')) {
// Remove leading spaces
if(!ingredient.empty() && ingredient[0] == ' ') {
ingredient = ingredient.substr(1);
}
if(!ingredient.empty()) {
uniqueIngredients.insert(ingredient);
}
}
std::cout << "Unique ingredients: ";
for(const auto& item : uniqueIngredients) {
std::cout << item << " ";
}
return 0;
}
Problem 2. Merging Shopping Lists
There are two shopping lists. You need to merge them without duplicates.
#include <iostream>
#include <set>
#include <string>
int main() {
std::set<std::string> mainList = {"cola", "chips", "pizza"};
std::set<std::string> guestList = {"juice", "chips", "nuts"};
// Add elements from the second list to the first
for(const auto& item : guestList) {
mainList.insert(item);
}
std::cout << "Final shopping list: ";
for(const auto& product : mainList) {
std::cout << product << " ";
}
// Output: cola nuts pizza juice chips (all unique and sorted)
return 0;
}
Common Beginner Mistakes
- Confusing set with vector. You cannot access elements in a set by index (
mySet[0]is an error). Use aforloop or iterators. - Forgetting #include <set>. Without this line, the compiler will not understand what
std::setis. - Thinking insert will add an element even if it exists. No,
insertdoes not create duplicates. If you need to store duplicates, usestd::multiset. - Trying to modify an element in a set. Elements in a set are constant. You cannot write
*it = 5;. First delete the old one, then add the new one.
Final Example: Contact Address Book
Let's create a program that stores contact names (only unique