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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:48:48+00:00 2026-06-11T19:48:48+00:00

In many languages, there are generators that help to initialize collections. In C++, if

  • 0

In many languages, there are generators that help to initialize collections. In C++, if one wants to initialize a vector uniformly, one can write:

std::vector<int> vec(10, 42); // get 10 elements, each equals 42

What if one wants to generate different values on the fly? For example, initialize it with 10 random values, or consecutive numbers from 0 to 9? This syntax would be convenient, but it does not work in C++11:

int cnt = 0;
std::vector<int> vec(10, [&cnt]()->int { return cnt++;});

Is there a nice way to initialize a collection by iterative function calls? I currently use this ugly pattern (not much more readable/short than a loop):

std::vector<int> vec;
int cnt = 0;
std::generate_n(std::back_inserter(vec), 10, [&cnt]()->int { return cnt++;});

There is a thing that would help, and it would explain the lack of the first constructor. I can imagine an iterator that takes a function and number of calls, so that the constructor

vector ( InputIterator first, InputIterator last);

would be applicable. But I did not find anything like this in the standard library. Did I miss it? Is there another reason why the first constructor did not make it to the standard?

  • 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-11T19:48:50+00:00Added an answer on June 11, 2026 at 7:48 pm

    Sadly, there is no standard facility to do this.

    For your specific example, you could use Boost.Iterator’s counting_iterator like this:

    std::vector<int> v(boost::counting_iterator<int>(0),
        boost::counting_iterator<int>(10));
    

    Or even with Boost.Range like this:

    auto v(boost::copy_range<std::vector<int>>(boost::irange(0,10)));
    

    (copy_range will basically just return std::vector<int>(begin(range), end(range)) and is a great way to adopt full range construction to exisiting containers that only support range construction with two iterators.)


    Now, for the general purpose case with a generator function (like std::rand), there is the function_input_iterator. When incremented, it calls the generator and saves the result, which is then returned when dereferencing it.

    #include <vector>
    #include <iostream>
    #include <cmath>
    #include <boost/iterator/function_input_iterator.hpp>
    
    int main(){
      std::vector<int> v(boost::make_function_input_iterator(std::rand, 0),
          boost::make_function_input_iterator(std::rand,10));
      for(auto e : v)
        std::cout << e << " ";
    }
    

    Live example.

    Sadly, since function_input_iterator doesn’t use Boost.ResultOf, you need a function pointer or a function object type that has a nested result_type. Lambdas, for whatever reason, don’t have that. You could pass the lambda to a std::function (or boost::function) object, which defines that. Here’s an example with std::function. One can only hope that Boost.Iterator will make use of Boost.ResultOf someday, which will use decltype if BOOST_RESULT_OF_USE_DECLTYPE is defined.

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

Sidebar

Related Questions

Since there are many programming languages that can be used to develop a web
Many languages in Europe are inflectional. This means that one word can be written
There is so many option in each programming languages which can be mentioned in
C++ has std::vector and Java has ArrayList , and many other languages have their
Hey everyone, in many programming languages there is this great idiom that lets you
There are so many languages in web development that sometime I get confused which
In many programming languages there is the basic equals operator which will see if
This should be very simple question. There are many programming languages out there, compiled
In C# , Ruby , and many other languages you can denote a string
There are many lexical analyzer and parser generators out there - lex/flex and yacc/bison,

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.