Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,917 questions

11,028 answers

573 users

How to write elements from ostream_iterator and vector to ostream (stdout) in C++

1 Answer

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

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

template <typename T>
inline void print(const T &data)
{
	for (const auto &element : data) {
		cout << element << ' ';
	}
	cout << endl;
}

int main()
{
	std::ostream_iterator<int> oi(cout, "\n");

	*oi = 55;
	oi++;
	*oi = 66;
	oi++;
	*oi = 77;

	vector<int> vec = { 1, 2, 3, 4, 5 };

	copy(vec.cbegin(), vec.cend(), std::ostream_iterator<int>(cout, ","));

	cout << endl;

	print(vec);
}


/*
run:

55
66
77
1,2,3,4,5,
1 2 3 4 5

*/

 




answered Jan 24 by avibootz
...