In this article, we will discuss different ways to convert a vector into a set in C++.

## Convert a Vector to a Set in C++ using Range Based Constructor

In C++, set class provides a constructor which accepts a range i.e. [start, end). It creates a set from all the element in the given range i.e. from start to end-1. So, to create a set from all elements of a vector, pass the vector elements as range. For example,

#include<iostream> #include<vector> #include<set> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create a set from vector set<int> set_of_nums(numbers.begin(), numbers.end()); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }

Output:

1, 2, 3, 4, 7, 8, 9, 10,

Vector provides two functions,

**vector::begin()**-> Returns the iterator pointing to the start of the vector.**vector::end()**-> Returns the iterator pointing to the an element after the end of vector.

Then we passed iterators returned by begin() and end() to the set constructor, to create a set from the vector.

## Create an empty set and add vector elements to it using for_each() & lambda function

Create an empty set and then iterate over all elements of vector and insert them one by one into the set. For iteration over all elements of vector, use the for_each() algorithm and pass a lambda function as argument.

for_each() will apply this lambda function on each of the element in vector. Inside the lambda function we can push the received element into the set. For example,

### Frequently Asked:

- Check if a vector is sorted in C++
- Check if two vectors are equal in C++
- How to fill a vector with random numbers in C++
- c++ std::vector and Iterator Invalidation example

#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; // Iterate over vector and add elements to set one by one for_each( numbers.begin(), numbers.end(), [&](const auto & elem){ set_of_nums.insert(elem); }); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }

Output:

1, 2, 3, 4, 7, 8, 9, 10,

## Create an empty set and ass vector items using copy()

Create an empty set. Then pass the vector elements as range [start, end) to the copy() function as initial two argument and as the third argument pass the back_insert_iterator of the set. It will copy all the elements of vector into the set. For example,

#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; // Iterate over vector and copy elements to set one by one copy( numbers.begin(), numbers.end(), std::inserter( set_of_nums, set_of_nums.end()) ); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }

Output:

1, 2, 3, 4, 7, 8, 9, 10,

## Create an empty set and fill with vector elements using generate_n()

generate_n() accepts 3 arguments:

**first**– the beginning of the range where elements needs to be generated**count**– number of the elements to be generated**callback**– A generator function object that will be called ‘count’ times to generate the value that will added in the container, whose iterator is provided as ‘first’.

So, we can pass a back_insert_iterator of set as first argument and size of vector (N) as the count argument. Then pass a lambda function as third argument.

This lambda function will be called N times and each time it returns the on each call it returns the ith element of the vector. Where **i** will start from 0 and go till N-1. For example,

#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; int i = 0; generate_n( inserter(set_of_nums, set_of_nums.begin()), numbers.size(), [&](){ return numbers[i++]; } ); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }

Output:

1, 2, 3, 4, 7, 8, 9, 10,

## C++: Create empty set and add vector items using transform()

In all the previous solutions, we copied elements from a vector to the set as it is. What if we want to change the elements while copying?

Suppose we have a vector of integers and we want to convert it to a set, but while adding them into the set, we want to multiple each element by 2. To that we can use the transform() function. For example,

#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; int main() { // Vector of integers vector<int> numbers{1, 3, 4, 7, 8, 9, 3, 2, 3, 1, 9, 8, 10}; // Create an empty set set<int> set_of_nums; transform ( numbers.begin(), numbers.end(), inserter(set_of_nums, set_of_nums.begin()), [](auto & elem){ return elem; }); // Iterate over the set and print all elements for(const auto & elem : set_of_nums) { cout<< elem << ", "; } return 0; }

Output

1, 2, 3, 4, 7, 8, 9, 10,

transform() function, accepts an input range, an output iterator and a unary function. It the then iterates over all the elements in input range and applies the unary function on it and gives the result to output iterator.

As the input range we gave the vector elements and then as output iterator we passed the set iterator and as the unary function we passed a lambda function which returns the argument by multiplying it by two.

so, transform() function iterated over all the elements in vector and on each element it applied the lambda function and then added them to the set.

**Summary:**

We learned about different ways to convert a vactor into a set in C++.