C++

# How to Change a Set to a Vector in C++

A set can be created in C++ as follows:

set<char> p = {'J', 'I', 'H', 'G', 'F'};

After this, there is internal sorting and the values of the set become arranged as follows based on the default settings:

'F', 'G', 'H', 'I', 'J'

When the set is converted to a vector, this new arrangement is maintained until it is changed. To code the set in a C++ program, the set library has to be included. To code the vector in a C++ program, the vector library has to be included.

There are a number of ways to change a set into a vector. Three simple ways are explained in this article. Two of these methods to be explained in this article, deal with member functions of the vector class. The other method deals with the copy function of the algorithm library.

## Range from Set

A range of elements can be obtained from a set. This range would not include the last element indicated. The range comes out in two iterators of the same type for a set. The following program illustrates this:

#include <iostream>
#include <set>
using namespace std;

int main()
{
set<char> st = {'J', 'I', 'H', 'G', 'F'};
set<char>::iterator itB = st.begin();  set::iterator itE = st.end();
itB++; itE--;

for (set<char>::iterator it = itB; it != itE; it++)
cout << *it << ", ";
cout << endl;
return 0;
}

The output is:

G, H, I,

Remember that the values in the set had been rearranged in ascending order based on default settings after insertion. The iterator itB points just before the first element of the reordered set at first. The iterator itE points just beyond the last element of the reordered set at first. “itB++” then points to the second element, while “itE–” then points to the last element for the range. This last element will not be included in the range.

The for-loop prints out the range, [‘G’, ‘H’, ‘I’[ , excluding ‘J’ as it should.

In the case of converting the whole set into a vector, the whole range of the set has to be used. So, itB or itE should neither be incremented nor decremented.

## The Range Vector Constructor

The vector constructor, that takes a range as arguments, is:

template<class InputIterator>
constexpr vector(InputIterator first, InputIterator last, const Allocator& = Allocator());

If the third argument is not given, the default value is chosen by C++. Comparing this syntax with the above code, first would be itB and last would be itE.

This constructor can therefore be used in converting a set into a vector. The following program illustrates this:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main()
{
set<char> st = {'J', 'I', 'H', 'G', 'F'};
set<char>::iterator itB = st.begin();  set::iterator itE = st.end();

vector<char> vtr(itB, itE);

for (int i=0; i<vtr.size(); i++)
cout << vtr[i] << ", ";
cout << endl;
return 0;
}

The output is:

F, G, H, I, J,

sorted. The Allocator argument was omitted in the code. The square brackets operator was used to obtain the values of the vector which were the sorted values from the set.

This has been one way of converting or changing a set into a vector. The other two ways are explained next:

## Vector Assign Member Function

One of the syntaxes for the vector assign() member function is:

template<class InputIterator>
constexpr void assign(InputIterator first, InputIterator last)

It takes a range as arguments, first and last for the same set iterator. In this situation, the empty vector has to be constructed first. After that, the assign method will add all the elements of the set to the vector. The set content remains unchanged but still sorted. The following program illustrates the use of the assign member function:

#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main()
{
set<char> st = {'J', 'I', 'H', 'G', 'F'};
set<char>::iterator itB = st.begin();  set<char>::iterator itE = st.end();

vector<char> vtr;
vtr.assign(itB, itE);

for (set<char>::iterator it = itB; it != itE; it++) cout << *it << ", "; cout << endl;
for (int i=0; i<vtr.size(); i++) cout << vtr[i] << ", ";cout << endl;
return 0;
}

The output is:

F, G, H, I, J,
F, G, H, I, J,

The first for-loop is to display the set content that is unchanged. The second is to display the vector whose content at the beginning is that of the sorted set.

This has been the second method to convert or change a set to a vector. The explanation for the third method for this article follows:

## A copy() Function in Algorithm’s Library

The syntax of one of the copy functions in the algorithm library, is:

template<class InputIterator, class OutputIterator>
constexpr OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)

In the case of vector, the return iterator is both an input iterator and an output iterator at the same time. If p is an input iterator, then *p would return the value pointed to by p. If p is an output iterator, then *p can receive a value for the memory location pointed to by p.

The first and second arguments here, are the same as for the previous function. The argument result is an OutputIterator that points to the first element of the vector.

The return OutputIterator here, points just after the last element of the vector. This means that the vector has to be created with a size that is at least equal to the size of the set.

With this copy() function, the algorithm library has to be included into the program because the function is in the algorithm library. The following code in the C++ main() function, shows how to use the copy function:

set<char > st = {'J', 'I', 'H', 'G', 'F'};
set<char >::iterator itB = st.begin();  set::iterator itE = st.end();

vector<char > vtr(10);
vector<char >::iterator outIt = copy(itB, itE, vtr.begin());
vtr.resize(outIt - vtr.begin());

for (set<char >::iterator it = itB; it != itE; it++) cout << *it << ", "; cout << endl;
for (int i=0; i<vtr.size(); i++) cout << vtr[i] << ", ";cout << endl;
return 0;

The output is:

F, G, H, I, J,
F, G, H, I, J,

The OutputIterator returned is of the vector. The vector had to be resized to the number of elements that are in the set. The content of the set did not change.

## Conclusion

A set can be changed into a vector using the range vector constructor or the vector assign() member function or the algorithm library copy() function. There are other less easy to code methods – see later. 