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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:13:57+00:00 2026-05-21T20:13:57+00:00

Suppose one wants to fill a vector with random numbers. Then there is a

  • 0

Suppose one wants to fill a vector with random numbers. Then there is a following obvious solution:

vector<int> result;
result.resize(n);
for (int i = 0; i < n; ++i) {
    result[i] = generateRandomNumber();
}

OK, it obviously works, but I would like to understand what is the simplest STL/Boost way to get rid of the for loop. It is tempting to use std::transform, but it takes a function with one argument. Is there any nice STL way to introduce a dummy argument in a function?

  • 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-21T20:13:57+00:00Added an answer on May 21, 2026 at 8:13 pm

    The C++ standard library has std::generate() and std::generate_n();

    For example:

    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    int generateRandomNumber()
    {
        return std::rand();
    }
    int main()
    {
        int n = 10;
        std::vector<int> result;
        generate_n(back_inserter(result), n, generateRandomNumber);
        copy(result.begin(), result.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << '\n';
    }
    

    test: https://ideone.com/5xD6P

    As for the second question, if I understand it correctly, is how to create a functor that takes an int argument, ignores it, and calls your int f()?

    C++98 way is to actually write the whole functor:

    struct IgnoreArgument
    {
        typedef int(*fp_t)();
        fp_t fp;
        IgnoreArgument(fp_t f) : fp(f) {}
        int operator()(int) const { return fp(); }
    };
    ...
    transform(v.begin(), v.end(), v.begin(), IgnoreArgument(f));
    

    test: https://ideone.com/DTsyl

    C++11 way is to use a lambda expression

    transform(v.begin(), v.end(), v.begin(), [](int){return f();});
    

    test: https://ideone.com/nAPXI

    And the C++98/boost way is to use boost::bind

    transform(v.begin(), v.end(), v.begin(), boost::bind(f));
    

    test: https://ideone.com/cvd88

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

Sidebar

Related Questions

Suppose I have one long long int and want to take its bits and
Suppose I have one list: IList<int> originalList = new List<int>(); originalList.add(1); originalList.add(5); originalList.add(10); And
Suppose one wants to build a novel generic class, Novel[A] . This class will
Suppose one wants to port a desktop app to a smartphone (which can mean
Suppose a programmer has the following problem: he wants to start a new python
Suppose I have the following objects (one table per object) with this relations: A
Suppose one wants to map over a collection, but only collect results of the
Suppose I have one html page with frames. The left frame is simply a
i am dynamically creating sql query for inserting multiple records in one hit.Suppose no
Suppose you have an ActiveRecord::Observer in one of your Ruby on Rails applications -

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.