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 1010395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:04:33+00:00 2026-05-16T09:04:33+00:00

I know how to do this in other languages, but not in C++, which

  • 0

I know how to do this in other languages, but not in C++, which I am forced to use here.

I have a set of strings (keywords) that I’m printing to out as a list, and the strings need a comma between them, but not a trailing comma. In Java, for instance, I would use a StringBuilder and just delete the comma off the end after I’ve built my string. How can I do it in C++?

auto iter = keywords.begin();
for (iter; iter != keywords.end( ); iter++ )
{
    out << *iter << ", ";
}
out << endl;

I initially tried inserting the following block to do it (moving the comma printing here):

if (iter++ != keywords.end())
    out << ", ";
iter--;
  • 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-16T09:04:34+00:00Added an answer on May 16, 2026 at 9:04 am

    Use an infix_iterator:

    // infix_iterator.h 
    // 
    // Lifted from Jerry Coffin's 's prefix_ostream_iterator 
    #if !defined(INFIX_ITERATOR_H_) 
    #define  INFIX_ITERATOR_H_ 
    #include <ostream> 
    #include <iterator> 
    template <class T, 
              class charT=char, 
              class traits=std::char_traits<charT> > 
    class infix_ostream_iterator : 
        public std::iterator<std::output_iterator_tag,void,void,void,void> 
    { 
        std::basic_ostream<charT,traits> *os; 
        charT const* delimiter; 
        bool first_elem; 
    public: 
        typedef charT char_type; 
        typedef traits traits_type; 
        typedef std::basic_ostream<charT,traits> ostream_type; 
        infix_ostream_iterator(ostream_type& s) 
            : os(&s),delimiter(0), first_elem(true) 
        {} 
        infix_ostream_iterator(ostream_type& s, charT const *d) 
            : os(&s),delimiter(d), first_elem(true) 
        {} 
        infix_ostream_iterator<T,charT,traits>& operator=(T const &item) 
        { 
            // Here's the only real change from ostream_iterator: 
            // Normally, the '*os << item;' would come before the 'if'. 
            if (!first_elem && delimiter != 0) 
                *os << delimiter; 
            *os << item; 
            first_elem = false; 
            return *this; 
        } 
        infix_ostream_iterator<T,charT,traits> &operator*() { 
            return *this; 
        } 
        infix_ostream_iterator<T,charT,traits> &operator++() { 
            return *this; 
        } 
        infix_ostream_iterator<T,charT,traits> &operator++(int) { 
            return *this; 
        } 
    };     
    #endif 
    

    Usage would be something like:

    #include "infix_iterator.h"
    
    // ...
    std::copy(keywords.begin(), keywords.end(), infix_iterator(out, ","));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this isn't strictly a programming question but y'all must have experienced this.
I know this is not programming directly, but it's regarding a development workstation I'm
I know this might be a no-brainer, but please read on. I also know
I know this is a broad question, but I've inherited several poor performers and
I know this site is written using ASP.Net MVC and I do not see
Anyone know this compiler feature? It seems GCC support that. How does it work?
I know this rather goes against the idea of enums, but is it possible
I know this would be easy with position:fixed, but unfortanately I'm stuck with supporting
This is not exactly a technical question, since I know C kind of enough
I'm very familiar with using Enums in other languages, but I'm having some difficulty

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.