I have a list of numbers. I would like to print them as comma separated list of numbers, at most 10 numbers per line. The following program snippet puts in the comma separated list of numbers, without using explicit for loop to iterate the vector of integers, may i be able to print 10 numbers at most per line?
std::vector<int> myvector;
for (int i=1; i<10; ++i) myvector.push_back(i*10);
stringstream ss;
std::copy(myvector.begin(), myvector.end(),
std::ostream_iterator<int>(ss, ", "));
cout << "list: " << ss.str() << endl;
The output appears as (with extra comma at the end):
list: 10, 20, 30, 40, 50, 60, 70, 80, 90,
I found a solution to my original problem:
// print at most 6 per line
int maxperline = 6;
std::vector<int>::const_iterator i1,i2,i3;
stringstream ss;
ss << "list: ";
for (i1 = myvector.begin(), i2 = myvector.end(); i1 < i2; i1+=maxperline) {
i3 = min(i2, i1+maxperline);
std::copy(i1, i3-1, std::ostream_iterator<int>(ss, ", "));
ss << *(i3-1) << '\n';
}
cout << '\n' << ss.str() << endl;
The output appears as:
list: 10, 20, 30, 40, 50, 60
70, 80, 90
In this approach, we can have flexibility by having maxperline set as value you desire