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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:07:57+00:00 2026-05-24T14:07:57+00:00

From time to time I am feeling the need for a certain kind of

  • 0

From time to time I am feeling the need for a certain kind of iterator (for which I can’t make up a good name except the one prefixed to the title of this question).

Suppose we have a function (or function object) that maps an integer to type T. That is, we have a definition of a mathematical sequence, but we don’t actually have it stored in memory. I want to make an iterator out of it. The iterator class would look something like this:

template <class F, class T>
class sequence_iterator : public std::iterator<...>
{
    int i;
    F f;
    public:
    sequence_iterator (F f, int i = 0):f(f), i(i){}
    //operators ==, ++, +, -, etc. will compare, increment, etc. the value of i.
    T operator*() const
    {
        return f(i);
    }    
};

template <class T, class F>
sequence_iterator<F, T> make_sequence_iterator(F f, int i)
{
    return sequence_iterator<F, T>(f, i);
}

Maybe I am being naive, but I personally feel that this iterator would be very useful. For example, suppose I have a function that checks whether a number is prime or not. And I want to count the number of primes in the interval [a,b]. I’d do this;

int identity(int i)
{
   return i;
}
count_if(make_sequence_iterator<int>(identity, a), make_sequence_iterator<int>(identity, b), isPrime);

Since I have discovered something that would be useful (at least IMHO) I am definitely positive that it exists in boost or the standard library. I just can’t find it. So, is there anything like this in boost?. In the very unlikely event that there actually isn’t, then I am going to write one – and in this case I’d like to know your opinion whether or not should I make the iterator_category random_access_iterator_tag. My concern is that this isn’t a real RAI, because operator* doesn’t return a reference.

Thanks in advance for any help.

  • 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-24T14:07:58+00:00Added an answer on May 24, 2026 at 2:07 pm

    boost::counting_iterator and boost::transform_iterator should do the trick:

    template <typename I, typename F>
    boost::transform_iterator<
        F,
        boost::counting_iterator<I>>
    make_sequence_iterator(I i, F f)
    {
        return boost::make_transform_iterator(
            boost::counting_iterator<I>(i), f);
    }
    

    Usage:

    std::copy(make_sequence_iterator(0, f), make_sequence_iterator(n, f), out);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From time to time I read that Fortran is or can be faster then
From time to time, I need to dump USB traffic under Windows, mostly to
From time to time I need to run a full build of the entire
From time to time I see an enum like the following: [Flags] public enum
From time to time I get a System.Threading.ThreadStateException when attempting to restart a thread.
From time to time I browse the web and look for interesting algorithms and
From time to time, I run into communications issue with other programmers, when we
From time to time my applications GUI stops redrawing. There a lot of threads
From time to time, a file that I'm interested in is modified by some
From time to time I see something like this: class Clazz { private int

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.