Convert Set to Vector in C++
Last Updated :
23 Jul, 2025
In C++, std::vectors stores data in the contiguous memory location while std::set stores data in non-contiguous memory location but in some specified order. In this article, we will learn different methods to convert the set to vector in C++.
Using Range Constructor of std::vector
The easiest way to convert the std::set to std::vector is by using the range constructor of std::vector. We just have to pass the iterator to the beginning and end of the std::set container to the ranged constructor of std::vector during its declaration.
Example
C++
// C++ program to Convert Set To Vector using
// range constructor
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = { 1, 2, 3, 7, 9, 5 };
// Copy all the elements of set into vector
// constructor range
vector<int> v(s.begin(), s.end());
for (int i : v)
cout << i << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in set.
Auxiliary Space: O(n)
Using vector::assign() Method
The std::vector::assign() function replaces the current vector elements from the elements of some other valid containers within a specific rang. We can assign the std::set elements to std::vector by passing the iterator to the start and end of the vector to the vector::assign() function.
Example
C++
// C++ program to convert set to vector using
// std::vector::assign()
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {1, 2, 3, 7, 9, 5};
// Create a vector
vector<int> v;
// Assigning all elements of set into vector
// using std::vector::assign() method
v.assign(s.begin(), s.end());
for (int i : v)
cout << i << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in set.
Auxiliary Space: O(n)
Using std:: copy() Function
The std::copy() inserts elements from a source container into a destination container. The destination container should be large enough to store all elements. Otherwise, we can use std::back_inserter() iterator instead of destination that works is similar to vector::push_back() function.
Example
C++
// C++ program to Convert Set to Vector using
// std::copy() function
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = { 1, 2, 3, 7, 9, 5 };
// Create a vector
vector<int> v;
// Copying all elements to set into vector
// using std::copy() method
copy(s.begin(), s.end(), back_inserter(v));
for (int i : v)
cout << i << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in set.
Auxiliary Space: O(n)
Manually Using vector::push_back() Method
We can manually push all the element of set one-by-one into vector using std::push_back() method using a loop. We can use the set::iterators to iterate to access each element of the set or we can use range based for loop.
Example
C++
// C++ program to convert set to vector using
// std::push_back() method
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = { 1, 2, 3, 7, 9, 5 };
vector<int> v;
// Inserting elements into vector using
// push_back function
for (auto i: s)
v.push_back(i);
for (int i : v)
cout << i << " ";
return 0;
}
Time Complexity: O(n), where n is the number of elements in set.
Auxiliary Space: O(n)
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems