How to convert a Vector to Set in C++ - GeeksforGeeks

16 Apr, 2020
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 SolutionGet 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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the  set    set<int> s;      // Traverse the Vector    for (int x : v) {          // Insert each element        // into the Set        s.insert(x);    }      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}Output:Vector: 1 2 3 1 1 
Set: 1 2 3

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.
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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the  set    set<int> s;      // Traverse the Vector    for (int x : v) {          // Insert each element        // into the Set        s.insert(x);    }      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}
Vector: 1 2 3 1 1 
Set: 1 2 3

Method 2: Using Range ConverterGet 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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the set    // using range of vector    set<int> s(v.begin(), v.end());      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}Output:Vector: 1 2 3 1 1 
Set: 1 2 3

Get the vector.Define a set which copies all elements of the vector using 2 pointers begin and end.Print the set.
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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the set    // using range of vector    set<int> s(v.begin(), v.end());      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}
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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the set    set<int> s;      // Inserting the elements    // of the vector in the set    // using copy() method    copy(          // From point of the destination        s.begin(),          // From point of the destination        s.end(),          // Method of copying        back_inserter(v));      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}Output:Vector: 1 2 3 1 1 
Set:

Get the vector.Define a set which copies all elements of the vector copy() method.Print the set.
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>using namespace std;  // Function to convert Vector to Setset<int> convertToSet(vector<int> v){    // Declaring the set    set<int> s;      // Inserting the elements    // of the vector in the set    // using copy() method    copy(          // From point of the destination        s.begin(),          // From point of the destination        s.end(),          // Method of copying        back_inserter(v));      // Return the resultant Set    return s;}  // Functiont for printing the setvoid printSet(set<int> s){      cout << "Set: ";    for (int x : s) {        cout << x << " ";    }    cout << endl;}  // Functiont for printing the vectorvoid printVector(vector<int> vec){      cout << "Vector: ";    for (int x : vec) {        cout << x << " ";    }    cout << endl;}  // Driver Codeint main(){      // Vector    vector<int> vec = { 1, 2, 3, 1, 1 };    printVector(vec);      // Convert Vector to Set    set<int> s = convertToSet(vec);      printSet(s);      return 0;}
Vector: 1 2 3 1 1 
Set:

cpp-set
cpp-vector
C++ Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
Iterative Letter Combinations of a Phone Number
cout in C++
Program to implement Singly Linked List in C++ using class
Dynamic _Cast in C++
How to sort an Array in descending order using STL in C++?
Why it is important to write "using namespace std" in C++ program?
Setting up Sublime Text for C++ Competitive Programming Environment
return 0 vs return 1 in C++