Skip to content
Related Articles

Related Articles

How to convert a Vector to Set in C++

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 01 Jun, 2022

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
    1. Get the vector to be converted.
    2. Create an empty set, to store the result.
    3. Iterate through the vector one by one, and insert each element into the set.
    4. Print the resultant set.

C++




// C++ program to convert
// a Vector to Set
 
#include <iostream>
#include <set>
#include <vector>
using namespace std;
 
// Function to convert Vector to Set
set<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 set
void printSet(set<int> s)
{
 
    cout << "Set: ";
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Functiont for printing the vector
void printVector(vector<int> vec)
{
 
    cout << "Vector: ";
    for (int x : vec) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Driver Code
int 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 
  • Method 2: Using Range Converter
    1. Get the vector.
    2. Define a set that copies all elements of the vector using 2 pointers begin and end.
    3. Print the set.

C++




// C++ program to convert
// a Vector to Set
 
#include <iostream>
#include <set>
#include <vector>
using namespace std;
 
// Function to convert Vector to Set
set<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 set
void printSet(set<int> s)
{
 
    cout << "Set: ";
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Functiont for printing the vector
void printVector(vector<int> vec)
{
 
    cout << "Vector: ";
    for (int x : vec) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Driver Code
int 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 
  • Method 3: Using Copy():
    1. Get the vector.
    2. Define a set that copies all elements of the vector using the standard algorithm std::copy() method defined in the <algorithm> header.
    3. Print the set.

Below is the implementation of the above approach:

C++




// C++ program to convert
// a Vector to Set
 
#include <iostream>
#include <set>
#include <vector>
using namespace std;
 
// Function to convert Vector to Set
set<int> convertToSet(vector<int> v)
{
    // Declaring the set
    set<int> s;
 
    // Inserting the elements
    // of the vector in the set
    // using copy() method
    copy(
 
        // The pointer to the beginning
        // of the source container
        v.begin(),
       
        // The pointer to the end
        // of the source container
        v.end(),
 
        // Method of copying
        inserter(s, s.end()));
 
    // Return the resultant Set
    return s;
}
 
// Functiont for printing the set
void printSet(set<int> s)
{
 
    cout << "Set: ";
    for (int x : s) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Functiont for printing the vector
void printVector(vector<int> vec)
{
 
    cout << "Vector: ";
    for (int x : vec) {
        cout << x << " ";
    }
    cout << endl;
}
 
// Driver Code
int 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 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!