convert set to vector c++
Converting Set to Vector in C++
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
int main() {
std::set<int> mySet = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::vector<int> myVector(mySet.begin(), mySet.end());
for (int num : myVector) {
std::cout << num << " ";
}
return 0;
}
Include necessary libraries:
cpp #include <iostream> #include <set> #include <vector> #include <algorithm>
Create a set and initialize it with some values:
cpp std::set<int> mySet = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
Convert the set to a vector using the range constructor:
cpp std::vector<int> myVector(mySet.begin(), mySet.end());
Iterate through the vector to display its elements:
cpp for (int num : myVector) { std::cout << num << " "; }