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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:38:54+00:00 2026-05-20T02:38:54+00:00

In STL/Boost, is there a ready-made output iterator that only counts the number of

  • 0

In STL/Boost, is there a ready-made output iterator that only counts the number of times it’s incremented? When an algorithm does *iter = value, the value would simply be discarded.

If not, then rolling my own seems easy enough. Although it might be useful to others if someone posted an example of such an iterator.

  • 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-20T02:38:55+00:00Added an answer on May 20, 2026 at 2:38 am

    No such iterator exists in standard C++, and to the best of my knowledge no Boost iterator has this exact functionality. There are plenty of ways that you could do this using those libraries without having to roll your own, though. For example, using Boost’s function_output_iterator, you could build a counter like this:

    struct Counter {
        size_t* out;
    
        explicit Counter(size_t* where) : out(where) {
            // Handled in initializer list
        }
    
        /* Either make this a template, or make the class itself a template. */
        template <typename T> void operator()(T& value) {
            ++ *out;
        }
    };
    

    This functor type takes in a pointer to a counter variable, and then whenever its operator() is invoked increments the counter. If you then wrap this in a function_output_iterator, as seen here:

    size_t count;
    your_algorithm(begin, end,
                  boost::make_function_output_iterator(Counter(&count)));
    

    Then whenever the created iterator is written to, your operator() will get called and the counter will get incremented.

    Hope this helps!

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

Sidebar

Related Questions

Is there an STL/boost algorithm that will test if all the elements between two
Is there a data structure (readily available in STL or boost), that accepts two
Is there a C/C++/STL/Boost clean method to convert a date time string to epoch
I was wondering if there is a native C++ (or STL/Boost) function which will
Are there any bridges to make mixing Qt with STL and Boost as seamless
Debugging with gdb, any c++ code that uses STL/boost is still a nightmare. Anyone
The STL commonly defines an output iterator like so: template<class Cont> class insert_iterator :
Is there in the STL or in Boost a set of generic simple comparison
Is there C++ equivalent for python Xrange generator in either STL or boost? xrange
Is there an algorithm in the standard library that can add a value to

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.