How to print vector using ostream_iterator in C++

1 Answer

0 votes
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using std::cout;
using std::endl;
using std::vector;

int main()
{
	vector<int> vec = { 0, 1, 2, 3, 4, 5 };

	std::ostream_iterator<int> output(cout, " ");

	std::copy(vec.begin(), vec.end(), output);

	cout << endl;

	return 0;
}

/*
run:

0 1 2 3 4 5

*/

 



answered Feb 26, 2018 by avibootz
edited Feb 27, 2018 by avibootz

Related questions

1 answer 156 views
1 answer 118 views
1 answer 168 views
1 answer 113 views
1 answer 96 views
1 answer 165 views
...