A set in C++ is a part of the Standard Template Library (STL) and is used to store unique elements. It is an associative container that automatically orders its elements.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
mySet.insert(1);
mySet.insert(2);
mySet.insert(3);
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
The elements in a set are always unique, and they are stored in a specific order (ascending by default).
Console Output:
1 2 3
Elements can be inserted into a set using the insert() function. If the element already exists, it will not be added again.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet;
mySet.insert(5);
mySet.insert(5); // Duplicate, won't be added
mySet.insert(10);
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
Attempting to insert a duplicate value (5) does not change the set, as sets do not allow duplicate elements.
Console Output:
5 10
Elements can be removed from a set using the erase() function. You can remove elements by value or by iterator.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4};
mySet.erase(2); // Remove element by value
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
The element '2' is removed from the set. The remaining elements are printed in ascending order.
Console Output:
1 3 4
To check if an element exists in a set, use the find() function. It returns an iterator to the element if found, otherwise it returns the end iterator.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4};
if (mySet.find(3) != mySet.end()) {
std::cout << "3 is in the set" << std::endl;
} else {
std::cout << "3 is not in the set" << std::endl;
}
return 0;
}
The find() method checks for the existence of the element '3' in the set. Since it is present, the output confirms its presence.
Console Output:
3 is in the set
You can iterate over elements in a set using iterators, which provide a way to access each element sequentially.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {10, 20, 30, 40};
for (auto it = mySet.begin(); it != mySet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
This example demonstrates how to iterate over a set using an iterator. Each element of the set is accessed in ascending order.
Console Output:
10 20 30 40
The size() function returns the number of elements currently stored in the set.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
std::cout << "The size of the set is: " << mySet.size() << std::endl;
return 0;
}
The size() method is used to determine the number of elements in the set, which is 5 in this example.
Console Output:
The size of the set is: 5
The clear() function removes all elements from the set, leaving it empty.
#include <iostream>
#include <set>
int main() {
std::set<int> mySet = {1, 2, 3, 4, 5};
mySet.clear();
std::cout << "The set is now empty with size: " << mySet.size() << std::endl;
return 0;
}
After clearing the set, its size becomes 0, indicating that all elements have been removed.
Console Output:
The set is now empty with size: 0
To perform a union of two sets, you can insert elements of one set into another. This combines both sets into one, preserving unique elements.
#include <iostream>
#include <set>
int main() {
std::set<int> set1 = {1, 2, 3};
std::set<int> set2 = {3, 4, 5};
set1.insert(set2.begin(), set2.end());
for (auto it = set1.begin(); it != set1.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
This example demonstrates the union of two sets, resulting in a set containing all unique elements from both sets.
Console Output:
1 2 3 4 5
To find the intersection of two sets, iterate through one set and check for each element's presence in the other set.
#include <iostream>
#include <set>
int main() {
std::set<int> set1 = {1, 2, 3, 4};
std::set<int> set2 = {3, 4, 5, 6};
std::set<int> resultSet;
for (auto it = set1.begin(); it != set1.end(); ++it) {
if (set2.find(*it) != set2.end()) {
resultSet.insert(*it);
}
}
for (auto it = resultSet.begin(); it != resultSet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
The intersection operation results in a set containing elements common to both sets, which are '3' and '4'.
Console Output:
3 4
The difference between two sets can be found by iterating through one set and inserting elements not found in the other set into a result set.
#include <iostream>
#include <set>
int main() {
std::set<int> set1 = {1, 2, 3, 4};
std::set<int> set2 = {3, 4, 5, 6};
std::set<int> resultSet;
for (auto it = set1.begin(); it != set1.end(); ++it) {
if (set2.find(*it) == set2.end()) {
resultSet.insert(*it);
}
}
for (auto it = resultSet.begin(); it != resultSet.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
The difference operation results in a set containing elements from 'set1' that are not present in 'set2', which are '1' and '2'.
Console Output:
1 2
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies