0

Ok, so by reading this question I've understood what \b and \n do exactly. I still don't know how to do the following:

I need to print all elements of an array separated by a space, then do some operation on that array and print another set of all elements from the array, a number of times, with each set separated by a newline. Something like this:

x[0] x[1] x[2] x[n]
x[0] x[1] x[2] x[n]
x[0] x[1] x[2] x[n]
(etc.)

This is my code:

std::printf("%f ",t);
for(int i=0;i<N;i++) {
    std::printf("%f ", x[i]);
}
std::printf("\b\n");

However this leaves a trailing space at the end of each line. How can I make the output be "x[0] x[1] x[n]\nx[0] x[1] x[n]\n(...)" instead of "x[0] x[1] x[n] \nx[0] x[1] x[n] \n(...)"?

Community
  • 1
  • 1
andrepd
  • 587
  • 6
  • 20

3 Answers3

3

I'd use an infix_ostream_iterator.

Using that, the code becomes fairly simple:

#include "infix_iterator"

std::cout << t << " ";
std::copy(x.begin(), x.end(), infix_ostream_iterator<double>(std::cout, " "));
std::cout << "\n";
Community
  • 1
  • 1
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Add a space only when you are not writing the last element.

std::printf("%f ",t);
for(int i=0;i<N;i++) {
    std::printf("%f", x[i]);
    if ( i != N-1 )
    {
       std::printf(" ");
    }
}
std::printf("\n");
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Well, this does the job but I'm writing a high performance program and this printing cycle is inserted in a loop that iterates tens of millions of times, with the array having 20-30 elements , so this means a number of comparisons in the order of 10^8 to 10^9. Maybe it's silly and it's actually insignificant, but I'm worried about the performance impact. – andrepd Jan 17 '15 at 00:27
  • The bottleneck will definitely be disk IO, not the comparison code. Give it a shot and see what you find. – R Sahu Jan 17 '15 at 00:32
  • Actually, replacing my code with yours makes it go *faster*, 2.5s vs 1.8s for a small number of iterations. o.O – andrepd Jan 17 '15 at 00:45
  • Nevermind I accidentally omitted compiler flags on one case. Difference is unmeasurable on this scale. – andrepd Jan 17 '15 at 00:47
1

Check whether you're printing the last number before printing the space after the number:

for (int i = 0; i < N; i++) {
    std::printf("%f", x[i]);
    if (i != N-1) {
        std::putc(' ');
    }
}

If your benchmarks show that the comparisons are the bottleneck, rather than I/O, you can split up the loop and handle the last case separately:

int i;
for (i = 0; i < N-1; i++) {
    std::printf("%f ", x[i]);
}
if (i < N) {
    std::printf("%f", x[i]);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Well, this does the job but I'm writing a high performance program and this printing cycle is inserted in a loop that iterates tens of millions of times, with the array having 20-30 elements , so this means a number of comparisons in the order of 10^8 to 10^9. Maybe it's silly and it's actually insignificant, but I'm worried about the performance impact. – andrepd Jan 17 '15 at 00:27
  • 1
    Compared to the actual I/O overhead, the comparisons are negligible. – Barmar Jan 17 '15 at 00:30
  • Bingo. This was exactly what I was looking for, thanks. (couldn't the if block be just `std::printf("%f", x[N]);`?) – andrepd Jan 17 '15 at 00:35
  • It should be `std::printf("%f", x[N-1]);`. But if `N == 0`, the array is empty so there's nothing to print. – Barmar Jan 17 '15 at 00:36