set in C++

Elements of the set are constant. 


Insert Element in set

#include <iostream>
#include <set>

int main()
{
    std::set<int> orderedSet;
    orderedSet.insert(1);
    orderedSet.insert(2);
    
    for (auto i : orderedSet) {
            std::cout << i << std::endl;
    }

    return 0;
}

Output 

1
2

Reinitialise the set/Initialisation of list

#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;
    }
    return 0;
}

Output 

1

2

 

0

7

8               

Duplicates will not be added. 

Search element by value or by Index

#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;
}

Output:

1
2
3
1
2

0
7
8
6 is not found
0 is found at Index 0
8 is found at Index 2

Click here to debug.



Assign set to another empty-set 

https://stackoverflow.com/questions/11056017/how-to-add-elements-of-a-stdset-to-another-non-empty-set


Check if set is empty or not

https://www.javatpoint.com/cpp-set-empty-function#:~:text=%E2%86%92%20%E2%86%90%20prev-,C%2B%2B%20set%20empty(),)%20otherwise%2C%20it%20returns%20false.

Clear the set elements

https://www.geeksforgeeks.org/setclear-c-stl/#:~:text=set%3A%3Aclear(),thus%20making%20its%20size%200.


Number of elements in the size

https://www.geeksforgeeks.org/setsize-c-stl/#:~:text=size()%20function%20is%20used,elements%20in%20the%20set%20container.&text=Return%20Value%3A%20It%20returns%20the,elements%20in%20the%20set%20container.

Extract elements from the set

https://godbolt.org/z/nnxceYPaY


How to change element from set? or modifying the elements in the set? 

1) https://stackoverflow.com/questions/24185573/change-element-in-a-set-via-iterator

2) https://stackoverflow.com/questions/65822046/cant-assign-to-return-value-because-function-operator-returns-a-const-value

3) https://stackoverflow.com/questions/31153825/modifying-elements-in-stdset

4) Modifying the set elements using C++17 features

#include <iostream>
#include <set>
using namespace std;

int main()
{
std::set<std::string> charset{"d", "b", "c"};
for (auto c: charset) { std::cout << c << " "; }
auto vertex = charset.extract(charset.begin());
std::string& str = vertex.value();
str = "a";
std::cout << "\nAfter assigning to a new char\n" ;
for (auto c: charset) { std::cout << c << " "; }
charset.insert(std::move(vertex));
std::cout << "\nNew char set\n";
for (auto c: charset) { std::cout << c << " "; }
return 0;
}

Output:

1
2
3
b c d
After assigning to a new char
c d
New char set
a c d

Click here to debug

Set with User Defined Data type 

https://stackoverflow.com/questions/1114856/stdset-with-user-defined-type-how-to-ensure-no-duplicates


Reference:

Accessing specific set element

Advance in C++20


Comments