1

For reference I found this in C++ Solutions: Companion to The C++ Programming Language by David Vandevoorde in the answer to exercise 19 in chapter 10

To output the results of the function, he used this code utilizing the copy algorithm to print the vectors out

map<string, vector<int> > *index = make_line_index(cin, entries);
vector<string>::iterator p = entries->begin();
for(; p!=entries.end(); ++p) {
   cout << "Word " << *p << " appears in lines ";
   map<string, vector<int> >::iterator lines = index->find(*p);
   std::copy((*p).second.being(), (*p).second.end(),
             ostream_iterator<int>(cout, ", "));
   cout << ".\n";
}

This seems like a pretty nice way to print out a vector but my only problem with this is the output ends up with a bit of formatting ugliness that I can't seem to get rid of

example output:

 Word cat appears in lines 1, 2, .
 Word dog appears in lines 3, .

I was wondering if there was a simple way to fix this and still use the algorithm or would it be better to go back to using a for loop and using an if statement to make sure I don't print ", " after the last entry.

I looked into rolling back cout but it seems like thats impossible. From what I gathered seekp() doesn't work with cout.

Or if anybody has another interesting way to print out vectors I'd be happy to hear it.

If anybody wants to see the rest of Vandervoorde's solution or the exercise from Stroustrup's book I'd be happy to post it(barring any copyright problems with posting full exercises and solutions from the books). I felt like it was unnecessary to post the rest of Vandervoorde's code

Also, this is my first question on here so if I did something wrong or frowned upon don't hesitate to inform me.

Thanks

  • Are you printing to the console? You could print a `'\b'` to erase the final comma, or just print the first before the loop and put the comma before each output instead of after. – chris Jun 09 '12 at 21:15
  • yea, I'm printing to the console. Can you do multiple \b's in a row? Because that sounds like exactly what I need. I'm assuming \b is a backspace character right? – Andy Entrekin Jun 09 '12 at 21:16
  • @chris: Good idea, but wouldn't that be `\b\b`? – Cameron Jun 09 '12 at 21:16
  • @Cameron, Yeah, I didn't catch the space after it. – chris Jun 09 '12 at 21:17

2 Answers2

3

You can use a different output iterator, for example, consider this very useful implementation of an infix_ostream_iterator from comp.lang.c++. It looks like Jerry Coffin also posted it here on Stack Overflow in an answer to "Printing lists with commas C++".

Community
  • 1
  • 1
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • That looks like it should be part of the standard. This loop problem comes up a lot. – chris Jun 09 '12 at 21:18
  • I'll probably go with just using \b for this exercise but this looks quite useful for future use. I agree with chris, it should be part of the standard – Andy Entrekin Jun 09 '12 at 21:23
1

IMHO you guys are making a simple thing needlessly complicated

//populate a vector
std::vector< int > vints;
for(int k= 0; k<4; k++ )
    vints.push_back( k );

// print out with commas between elements
// but not after last one
for( int k=0;k <vints.size()-1;k++)
    cout << vints[k] << ", ";
cout << vints.back() << endl;
ravenspoint
  • 19,093
  • 6
  • 57
  • 103