Convert Set To Vector in C++
Prerequisite:
Containers are meant to be storing elements according to the properties they have, but in some cases, we need to convert from one container to another. One such case is Converting Set to vector also we can apply the same methods for Converting unordered_set to vector if we don’t want elements to be sorted in order.
Set to Vector in C++
There are 4 methods to Convert a set into a vector:
- Using Range Constructor
- Using Push_back()
- Using Copy function
- Using vector::assign function
1. Range Constructor
One of the easiest ways will be to declare a vector variable using the range constructor within the whole range of the set. std::vector range constructor takes two input iterators pointing to the beginning and the end of an input sequence.
Below is the implementation of the above approach
C++
// C++ program to Convert Set To Vector // Using range constructor #include <bits/stdc++.h> using namespace std; int main() { // Set declared set< int > st = { 1, 2, 3, 7, 9, 5 }; cout << "Original Set elements\n" ; for ( int i : st) cout << i << " " ; cout << endl; // range constructor from // st.begin() to st.end() // Vector declared with values vector< int > vc(st.begin(), st.end()); cout << "Printing Vector after conversion\n" ; for ( int i : vc) cout << i << " " ; cout << endl; return 0; } |
Original Set elements 1 2 3 5 7 9 Printing Vector after conversion 1 2 3 5 7 9
2. std:: push_back() function
std::push_back() function in vector append the pushed element at the last position. We can iterate through the set and push each element in our vector.
Below is the implementation of the above approach
C++
// C++ program to Convert Set To Vector // using push_back() #include <bits/stdc++.h> using namespace std; int main() { // Set declared set< int > st = { 1, 2, 3, 7, 9, 5 }; cout << "Original Set elements\n" ; for ( int i : st) cout << i << " " ; cout << endl; // vector declared vector< int > vc; // Inserting elements into vector using // push_back function for ( auto & it : st) { vc.push_back(it); } cout << "Printing Vector after conversion\n" ; for ( int i : vc) cout << i << " " ; cout << endl; return 0; } |
Original Set elements 1 2 3 5 7 9 Printing Vector after conversion 1 2 3 5 7 9
3. std:: copy function
std::copy inserts elements from a source container into a destination container. The destination container should be large enough to store all elements.
Below is the implementation of the above approach
C++
// C++ program to Convert Set // To Vector using // std:: copy function #include <bits/stdc++.h> using namespace std; int main() { set< int > st = { 1, 2, 3, 7, 9, 5 }; cout << "Original Set elements\n" ; for ( int i : st) cout << i << " " ; cout << endl; vector< int > vc(st.size()); copy(st.begin(), st.end(), vc.begin()); cout << "Printing Vector after conversion\n" ; for ( int i : vc) cout << i << " " ; cout << endl; return 0; } |
Original Set elements 1 2 3 5 7 9 Printing Vector after conversion 1 2 3 5 7 9
If we don’t know the size of the set then we can use the std::back_inserter() function on an empty vector, it is the same as running a loop and push_back() each element
Below is the implementation of the above approach
C++
// C++ program to Convert Set To Vector // Using copy function #include <bits/stdc++.h> using namespace std; int main() { // Set declared set< int > st = { 1, 2, 3, 7, 9, 5 }; cout << "Original Set elements\n" ; for ( int i : st) cout << i << " " ; cout << endl; // Vector declared vector< int > vc; // Using copy std::copy(st.begin(), st.end(), back_inserter(vc)); cout << "Printing Vector after conversion\n" ; for ( int i : vc) cout << i << " " ; cout << endl; return 0; } |
Original Set elements 1 2 3 5 7 9 Printing Vector after conversion 1 2 3 5 7 9
4. std::vector::assign function
vector::assign function replaces the current vector elements from the elements of some other valid containers within a specific range
Below is the implementation of the above approach
C++
// C++ program to Convert Set To Vector // Using vector::assign #include <bits/stdc++.h> using namespace std; int main() { // Using Set set< int > st = { 1, 2, 3, 7, 9, 5 }; cout << "Original Set elements\n" ; for ( int i : st) cout << i << " " ; cout << endl; // Vector declared vector< int > vc; // Using vector::assign vc.assign(st.begin(), st.end()); cout << "Printing Vector after conversion\n" ; for ( int i : vc) cout << i << " " ; cout << endl; return 0; } |
Original Set elements 1 2 3 5 7 9 Printing Vector after conversion 1 2 3 5 7 9
Time complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...