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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:08:07+00:00 2026-06-14T16:08:07+00:00

I got a piece of code like this: template<class Iter> void printRange(Iter begin, Iter

  • 0

I got a piece of code like this:

template<class Iter>
void printRange(Iter begin, Iter last) {
    for (Iter i = begin; i != last; i++) {
        std::cout << *i << ", ";
    }
     outstd::cout << *last << ", ";
}

My question is: how can i print out the last element, too, elegantly? Couting the last element individually after the loop does not seem to be a nice and correct solution, … Is there a better way? Note that i cannot use the <= operator or boost or any stl specific feature either.

  • 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-14T16:08:09+00:00Added an answer on June 14, 2026 at 4:08 pm

    You can output the separator conditionally:

    template<class Iter>
    void printContainer(Iter first, Iter last) {
        for (Iter i = first, end = ++last; i != end; i++) {
            std::cout << (i == first ? "" : ", ") << *i;
        }
    }
    

    For a more standard [begin, end) half-open range:

    template<class Iter>
    void printContainer(Iter begin, Iter end) {
        for (Iter i = begin; i != end; i++) {
            std::cout << (i == begin ? "" : ", ") << *i;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a piece of code like this: class Lekcja { Calendar _date; public
I got a piece of code like this: foo = None def outer(): global
Hey, I've got this nice little piece of code, much like all the other
I've got an enum like this in an old piece of code: [Flags] public
i got a piece of code that so far returns me data like this
I have got a very nasty piece of code i would like to refactor
I've got this piece of code to toggle a side panel: $(.example_wrapper_panel_link).click(function() { $(.example_wrapper).addClass('example_wrapper_active');
I have got a piece of code, that should countdown some number (in this
so i got this piece of code. (currPosX is defined earlier) while (earliestDate <
I've got this piece of code in my jquery validation script: resetForm: function() {

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.