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

  • SEARCH
  • Home
  • 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 3222034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:56:42+00:00 2026-05-17T15:56:42+00:00

Usually, when I had to display a list of separated strings, I was doing

  • 0

Usually, when I had to display a list of separated strings, I was doing something like:

using namespace std;
vector<string> mylist; // Lets consider it has 3 elements : apple, banana and orange.

for (vector<string>::iterator item = mylist.begin(); item != mylist.end(); ++item)
{
  if (item == mylist.begin())
  {
    cout << *item;
  } else
  {
    cout << ", " << *item;
  }
}

Which outputs:

apple, banana, orange

I recently discovered std::ostream_iterator which if found really nice to use.

However with the following code:

copy(mylist.begin(), mylist.end(), ostream_iterator<string>(cout, ", "));

If get:

apple, banana, orange, 

Almost perfect, except for the extra ,. Is there an elegant way to handle the special “first (or last) element case” and to have the same output than the first code, without its “complexity” ?

  • 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-05-17T15:56:42+00:00Added an answer on May 17, 2026 at 3:56 pm

    Although not with std:

    cout << boost::algorithm::join(mylist, ", ");
    

    EDIT: No problem:

    cout << boost::algorithm::join(mylist | 
        boost::adaptors::transformed(boost::lexical_cast<string,int>), ", "
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually use Netbeans for PHP development, but have recently had to move to
I've always had trouble with retrieving values in API-returned arrays, usually because they are
I'm working on something and while I usually have no problems with c# i've
i had a working application until i changed the package name in the AndroidManifest.xml.
I had a working WCF published to IIS on my server. Recently I created
I had an issue recently (see my last question) that led me to take
So I had this project where at first I did a git add .
I have a simple problem but I haven't had any luck finding the solution
I build various web applications in PHP. Clients usually have the need for sending
I have the following problem. My application has a tab bar at the bottom

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.