Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8079189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:01:42+00:00 2026-06-05T16:01:42+00:00

For reference I found this in C++ Solutions: Companion to The C++ Programming Language

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:01:44+00:00Added an answer on June 5, 2026 at 4:01 pm

    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++”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to find any reference for this function, but I haven't found anything.
I found this thread as a reference: How do I wait for an asynchronously
I found this orientation test code below looking for JQTouch reference material. This works
Previously I did a Tutorial on OpenGL-ES 1.0. For reference, this can be found
[Update: I've found the API reference. The method used is below] <?php wp_delete_post( $postid,
I've got a very strange problem and tried several solutions found in the www,
I want to have script reference code like this displayed in a textarea so
I have recently re-organised our source control and found one of our solutions no
I know this is the most common problem in the history of programming but
The samples and documentation in question can be found here: http://static.springsource.org/spring-flex/docs/1.5.0.M1/ The reference doc

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.