How to copy a list to vector in C++

1 Answer

0 votes
#include <iostream>
#include <list>
#include <vector>

int main()
{
	std::list<int>   lst = { 1, 2, 3, 4, 5, 6, };
	std::vector<int> vec;

	vec.resize(lst.size());

	copy(lst.cbegin(), lst.cend(), vec.begin());          
	
	for (auto elem : vec) {
		std::cout << elem << ' ';
	}
	std::cout << std::endl;
	
	return 0;
}

/*
run:

1 2 3 4 5 6

*/

 



answered Dec 30, 2017 by avibootz

Related questions

2 answers 134 views
1 answer 91 views
1 answer 160 views
1 answer 125 views
1 answer 127 views
127 views asked May 12, 2021 by avibootz
...