You can use std::copy to copy from one container (your list) to another container (your vector):
#include <list>
#include <vector>
#include <iostream>
int main() {
std::list<int> list = {1, 2, 3, 4, 5};
std::vector<int> vec;
std::copy(std::begin(list), std::end(list), std::back_inserter(vec));
for (auto element: vec) {
std::cout << element << ", ";
}
std::cout << "\n";
}
https://godbolt.org/z/73rj494f4
You can also manually copy elements from one container to the other. However, while this is maybe easier to understand, it is not as elegant:
#include <list>
#include <vector>
#include <iostream>
int main() {
std::list<int> list = {1, 2, 3, 4, 5};
std::vector<int> vec;
vec.reserve(list.size()); // Optional.
// Copy the elements manually
for (auto element: list) {
vec.push_back(element);
}
// Manually print the elements in vec.
for (auto element: vec) {
std::cout << element << ", ";
}
std::cout << "\n";
}
https://godbolt.org/z/faf8ahr75