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

Convert a Set into a Vector in C++ using Range Based Constructor

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

#include<iostream>
#include<vector>
#include<set>

using namespace std;

int main()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create a vector from set elements
    vector<int> numbers{set_of_nums.begin(), set_of_nums.end()};

    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

Set provides two functions,

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

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

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

Create an empty vector and then iterate over all elements in set and push them one by one into the vector. For iteration over all elements of set, 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 set. Inside the lambda function we can push the recived element into the vector. For example,

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

int main()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Iterate over all elements in set and apply
    // the lambda func on each element. Which adds them
    // vector.
    for_each(   set_of_nums.begin(),
                set_of_nums.end(),
                [&](const auto & elem){
                    numbers.push_back(elem);
                });


    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

Create an empty vector and push set items using copy()

Create an empty vector. Then pass the set 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 vector. It will copy all the elements of set into the vector. For example,

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

int main()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Copy all elements of set to vector
    copy(   set_of_nums.begin(),
            set_of_nums.end(),
            inserter(numbers, numbers.begin()) );


    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

C++: Create empty vector and fill with items from set

Create an empty vector. Then pass the set elements as a range [start, end) into the assign() function of vector. Which copies them into the vector. For example,

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

int main()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Copy all elements of set to vector
    numbers.assign(set_of_nums.begin(), set_of_nums.end());


    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output

1, 3, 4, 7, 8, 9,

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

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

Suppose we have a set of integers and we want to convert it to a vector, but while adding them into the vector, 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()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    // Multiply all elements of set by two and
    // add them to vector.
    transform ( set_of_nums.begin(),
                set_of_nums.end(),
                inserter(numbers, numbers.begin()),
                [](auto & elem){
                    return elem * 2;
                });

    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output:

2, 6, 8, 14, 16, 18,

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 set elements and then as output iterator we passed the vector 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 set and on each element it applied the lambda function and then added them to the vector.

C++: Create empty vector and push set elements using range based for loop

The most simplest solution is to create an empty vector, then iterate over all elements of set using range based for loop and during iteration add each element into the vector. For example,

#include<iostream>
#include<vector>
#include<set>

using namespace std;

int main()
{
    // Create a vector of integers
    set<int> set_of_nums{1, 3, 4, 7, 8, 9};

    // Create an empty vector
    vector<int> numbers;

    for(auto & elem: set_of_nums) {
        numbers.push_back(elem);
    }

    // Iterate over the vector and print elements
    for(auto & elem: numbers) {
        cout<<elem<<", ";
    }

    return 0;
}

Output:

1, 3, 4, 7, 8, 9,

Summary:

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