This article shows how to convert a vector to a set in C++.
Example:
Input: vector = {1, 2, 3, 1, 1}
Output: set = {1, 2, 3}
Input: vector = {1, 2, 3, 3, 5}
Output: set = {1, 2, 3, 5}
Below are the various ways to do the required conversion:
- Method 1: Naive Solution
- Get the vector to be converted.
- Create an empty set, to store the result.
- Iterate through the vector one by one, and insert each element into the set.
- Print the resultant set.
Below is the implementation of the above approach
// C++ program to convert// a Vector to Set#include <iostream>#include <set>#include <vector>usingnamespacestd;// Function to convert Vector to Setset<int> convertToSet(vector<int> v){// Declaring the setset<int> s;// Traverse the Vectorfor(intx : v) {// Insert each element// into the Sets.insert(x);}// Return the resultant Setreturns;}// Functiont for printing the setvoidprintSet(set<int> s){cout <<"Set: ";for(intx : s) {cout << x <<" ";}cout << endl;}// Functiont for printing the vectorvoidprintVector(vector<int> vec){cout <<"Vector: ";for(intx : vec) {cout << x <<" ";}cout << endl;}// Driver Codeintmain(){// Vectorvector<int> vec = { 1, 2, 3, 1, 1 };printVector(vec);// Convert Vector to Setset<int> s = convertToSet(vec);printSet(s);return0;}chevron_rightfilter_noneOutput:
Vector: 1 2 3 1 1 Set: 1 2 3
- Method 2: Using Range Converter
- Get the vector.
- Define a set which copies all elements of the vector using 2 pointers begin and end.
- Print the set.
Below is the implementation of the above approach:
// C++ program to convert// a Vector to Set#include <iostream>#include <set>#include <vector>usingnamespacestd;// Function to convert Vector to Setset<int> convertToSet(vector<int> v){// Declaring the set// using range of vectorset<int> s(v.begin(), v.end());// Return the resultant Setreturns;}// Functiont for printing the setvoidprintSet(set<int> s){cout <<"Set: ";for(intx : s) {cout << x <<" ";}cout << endl;}// Functiont for printing the vectorvoidprintVector(vector<int> vec){cout <<"Vector: ";for(intx : vec) {cout << x <<" ";}cout << endl;}// Driver Codeintmain(){// Vectorvector<int> vec = { 1, 2, 3, 1, 1 };printVector(vec);// Convert Vector to Setset<int> s = convertToSet(vec);printSet(s);return0;}chevron_rightfilter_noneOutput:Vector: 1 2 3 1 1 Set: 1 2 3
- Method 3: Using Copy()
- Get the vector.
- Define a set which copies all elements of the vector copy() method.
- Print the set.
Below is the implementation of the above approach:
// C++ program to convert// a Vector to Set#include <iostream>#include <set>#include <vector>usingnamespacestd;// Function to convert Vector to Setset<int> convertToSet(vector<int> v){// Declaring the setset<int> s;// Inserting the elements// of the vector in the set// using copy() methodcopy(// From point of the destinations.begin(),// From point of the destinations.end(),// Method of copyingback_inserter(v));// Return the resultant Setreturns;}// Functiont for printing the setvoidprintSet(set<int> s){cout <<"Set: ";for(intx : s) {cout << x <<" ";}cout << endl;}// Functiont for printing the vectorvoidprintVector(vector<int> vec){cout <<"Vector: ";for(intx : vec) {cout << x <<" ";}cout << endl;}// Driver Codeintmain(){// Vectorvector<int> vec = { 1, 2, 3, 1, 1 };printVector(vec);// Convert Vector to Setset<int> s = convertToSet(vec);printSet(s);return0;}chevron_rightfilter_noneOutput:Vector: 1 2 3 1 1 Set:
Recommended Posts:
- vector::front() and vector::back() in C++ STL
- vector::empty() and vector::size() in C++ STL
- vector::push_back() and vector::pop_back() in C++ STL
- vector::operator= and vector::operator[ ] in C++ STL
- vector::at() and vector::swap() in C++ STL
- vector::crend() & vector::crbegin() with example
- vector::begin() and vector::end() in C++ STL
- vector :: cbegin() and vector :: cend() in C++ STL
- How to flatten a Vector of Vectors or 2D Vector in C++
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting 2D Vector in C++ | Set 3 (By number of columns)
- Common Subtleties in Vector STLs
- Ways to copy a vector in C++
- Vector in C++ STL
- Modifiers for Vector in C++ STL
- 2D vector in C++ with user defined size
- vector erase() and clear() in C++
- Using std::vector::reserve whenever possible
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

