Converting String into Set in C++ STL Last Updated : 07 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Prerequisites: String in C++Set STL in C++ A string is a collection of characters and if we are converting it into a set the only reason can be to check the characters being used without duplicate values. Example: string s="Geeks for Geeks is for Geeks" // G e k s f o r i are characters // set can store these characters in sorted order. So, to Perform this operation there are two methods : Passing the string into the set constructor.Iterating the string using for loop.1. Passing the string into the set constructor Using predefined properties can really be useful while converting strings into sets. We can just insert elements by putting a range of string elements inside the constructor for copying elements inside the set. Syntax: set <char>set_obj ( begin( string_name ) , end( string_name ) ) Code: C++ // C++ Program to Convert // String to set // Using set constructor #include <bits/stdc++.h> using namespace std; int main() { // Declaring the string string name = "geeksforgeeks"; // declaring the string // and passing the string // in the set constructor set<char> my_name(name.begin(), name.end()); // printing the set for (auto it : my_name) { cout << it << " "; } cout << endl; return 0; } Outpute f g k o r s Here the string 'name' is passed in the set constructor. And the characters in the string are converted into the set and then the elements are printed. Here the elements are printed in ascending order and there is no repetition of the elements(unique elements are printed). 2. Iterating the string using for loop The simplest way to perform the task is using for loop. First, we can iterate over the string using loops by which we can directly insert elements inside the set. Code: C++ // C++ Program to Convert // String into set // Using for loop #include <bits/stdc++.h> using namespace std; int main() { // Declaring the string string name = "jayadeep"; // Declaring the set set<char> s1; // inserting the string elements // into the set. for (int x = 0; x < name.size(); x++) { s1.insert(name[x]); } set<char>::iterator itr; // printing the set. for (itr = s1.begin(); itr != s1.end(); itr++) { cout << *itr; } cout << endl; } Outputadejpy Comment More infoAdvertise with us Next Article Converting String into Set in C++ STL hiteshreddy2181 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-set +1 More Practice Tags : CPPSTL Similar Reads Convert Set to Vector in C++ In C++, std::vectors stores data in the contiguous memory location while std::set stores data in non-contiguous memory location but in some specified order. In this article, we will learn different methods to convert the set to vector in C++.Table of ContentUsing Range Constructor of std::vectorUsin 3 min read set::insert() function in C++ STL The std::set::insert() is a built-in function of C++ STL set container which is used to insert new elements in it. In this article, we will learn how to use set::insert() function in our C++ programs.SyntaxThe string::replace() function provides 6 different overloads for different purposes:st.insert 4 min read Using erase_If with Set in C++ STL The erase_if function in C++20 is used to erase all the elements in the set satisfying the required condition. This condition is referred to as the predicate. The syntax for the erase_if function is erase_if(set, predicate); The first argument of erase function is the set in which elements need to b 2 min read set::count() Function in C++ STL The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read unordered_set insert() function in C++ STL The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at 3 min read unordered_set count() function in C++ STL The unordered_set::count() function is a built-in function in C++ STL which is used to count occurrences of a particular element in an unordered_set container. As the unordered_set container does not allows to store duplicate elements so this function is generally used to check if an element is pres 2 min read set emplace_hint() function in C++ STL The set::emplace_hint() is a built-in function in C++ STL which inserts a new element in the set. A position is passed in the parameter of the function which acts as a hint from where the searching operation starts before inserting the element at its current position. The position only helps the pro 2 min read set::clear in C++ STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. set::clear() clear() 2 min read set value_comp() function in C++ STL The set::value_comp() is an inbuilt function in cpp that returns a copy of the comparison object used by the container. This object determines the order of the elements in the container. It is a function pointer or a function object that takes two arguments of the same type as the container elements 2 min read unordered_set cbegin() function in C++ STL The unordered_set::cbegin() method is a built-in function in C++ STL which is used to return a const_iterator pointing to the first element in the unordered_set container. This iterator can point to either the very the first element or first element of any specified bucket in the unordered_set conta 2 min read Like