CPP: Convert Set to Vector

In the previous article, we have discussed about Convert Vector to Set. Let us learn How to Convert Set to Vector in C++ Program.

Vector:

Vectors are similar to dynamic arrays in that they can resize themselves when an element is added or removed, and the container takes care of their storage. Iterators can access and traverse vector elements since they’re stored in contiguous storage. Data is inserted at the top of vectors. Inserting at the end takes longer since the array may need to be extended at times. There is no resizing, and removing the final variable still takes the same amount of time. Inserting and erasing at the start or within the middle is linear in terms of your time .

Sets:

Because the value of an element identifies it, sets are a type of associative container in which each element must be unique. The element’s value cannot be changed after it has been added to the set, but the element’s modified value can be removed and re-added.

Given a set, the task is to convert the given set to vector

Examples:

Input:

given_set = { 9, 3, 2, 0, 8, 5, 2, 1, 7 }

Output:

0 1 2 3 5 7 8 9

Convert Set to Vector

There are several ways to convert the given set to vector some of them are:

Method #1: Basic approach

We can also write our own set-to-vector conversion routine. The concept is straightforward: start with an empty vector, traverse the set with a range-based for-loop, and insert each element into the vector.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating and initializing set with some random
    // integer values
    set<int> intset{ 9, 3, 2, 0, 8, 5, 2, 1, 7 };
    // Creating a vector
    vector<int> intvector;
    // Traverse the set and add element to vector
    for (auto element : intset) {
        intvector.push_back(element);
    }
    // Traverse the vector and print all the vector elements
    for (auto value : intvector)
        cout << value << " ";
    return 0;
}

Output:

0 1 2 3 5 7 8 9

Method #2:Using Range Based Constructor

The std::vector range constructor, which takes two input iterators pointing to the start and end of an input sequence, is the most elegant solution.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating and initializing set with some random
    // integer values
    set<int> intset{ 9, 3, 2, 0, 8, 5, 2, 1, 7 };
    // Converting set to vector

    vector<int> intvector(intset.begin(), intset.end());

    // Traverse the vector and print all the vector elements
    for (auto value : intvector)
        cout << value << " ";
    return 0;
}

Output:

0 1 2 3 5 7 8 9

Method #3:Using copy() method

Make a new empty vector. Then, as the first two arguments, pass the set elements as range [start, end] to the copy() function, and as the third argument, pass the vector’s back push_back iterator. It will copy all of the set’s elements into the vector.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating and initializing set with some random
    // integer values
    set<int> intset{ 9, 3, 2, 0, 8, 5, 2, 1, 7 };
    vector<int> intvector;
    // Converting set to vector
    // copy the set to vector using copy() method
    copy(intset.begin(), intset.end(),
         inserter(intvector, intvector.end()));

    // Traverse the vector and print all the vector elements
    for (auto value : intvector)
        cout << value << " ";
    return 0;
}

Output:

0 1 2 3 5 7 8 9

Method #4:Using for_each() & lambda

Make an empty vector, then iterate through all of the elements in set, pushing them one by one into the vector. Use the for each() algorithm with a lambda function as an argument to iterate over all elements of the set.

This lambda function will be applied to each element in set by for each(). We can insert the received element into the vector within the lambda function.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating and initializing set with some random
    // integer values
    set<int> intset{ 9, 3, 2, 0, 8, 5, 2, 1, 7 };
    vector<int> intvector;
    // using for_each and lambda fucntion convert set to
    // vector
    for_each(intset.begin(), intset.end(),
             [&](const auto& element) {
                 intvector.push_back(element);
             });

    // Traverse the vector and print all the vector elements
    for (auto value : intvector)
        cout << value << " ";
    return 0;
}

Output:

0 1 2 3 5 7 8 9

Method #5:Using assign()

Make a new vector that is empty. Then, pass the set elements as a range [start, end] into vector’s assign() function. That is, they are copied into the vector.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    // creating and initializing set with some random
    // integer values
    set<int> intset{ 9, 3, 2, 0, 8, 5, 2, 1, 7 };
    vector<int> intvector;
    // using assign to copy elements from set to vector
    intvector.assign(intset.begin(), intset.end());

    // Traverse the vector and print all the vector elements
    for (auto value : intvector)
        cout << value << " ";
    return 0;
}

Output:

0 1 2 3 5 7 8 9

Related Programs: