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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:45:25+00:00 2026-06-09T01:45:25+00:00

With C++11, the STL has now a std::iota function (see a reference ). In

  • 0

With C++11, the STL has now a std::iota function (see a reference). In contrast to std::fill_n, std::generate_n, there is no std::iota_n, however. What would be a good implementation for that? A direct loop (alternative 1) or delegation to std::generate_n with a simple lambda expression (alternative 2)?

Alternative 1)

template<class OutputIterator, class Size, class T>
OutputIterator iota_n(OutputIterator first, Size n, T value)
{
        while (n--)
                *first++ = value++;
        return first;
}

Alternative 2)

template<class OutputIterator, class Size, class T>
OutputIterator iota_n(OutputIterator first, Size n, T value)
{
        return std::generate_n(first, n, [&](){ return value++; });
}    

Would both alternatives generate equivalent code with optimizing compilers?

UPDATE: incorporated the excellent point of @Marc Mutz to also return the iterator at its destination point. This is also how std::generate_n got updated in C++11 compared to C++98.

  • 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-09T01:45:27+00:00Added an answer on June 9, 2026 at 1:45 am

    As a random example, I compiled the following code with g++ -S -O2 -masm=intel (GCC 4.7.1, x86_32):

    void fill_it_up(int n, int * p, int val)
    {
        asm volatile("DEBUG1");
        iota_n(p, n, val);
        asm volatile("DEBUG2");
        iota_m(p, n, val);
        asm volatile("DEBUG3");
        for (int i = 0; i != n; ++i) { *p++ = val++; }
        asm volatile("DEBUG4");
    }
    

    Here iota_n is the first version and iota_m the second. The assembly is in all three cases this:

        test    edi, edi
        jle .L4
        mov edx, eax
        neg edx
        lea ebx, [esi+edx*4]
        mov edx, eax
        lea ebp, [edi+eax]
        .p2align 4,,7
        .p2align 3
    .L9:
        lea ecx, [edx+1]
        cmp ecx, ebp
        mov DWORD PTR [ebx-4+ecx*4], edx
        mov edx, ecx
        jne .L9
    

    With -O3, the three versions are also very similar, but a lot longer (using conditional moves and punpcklqdq and such like).

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

Sidebar

Related Questions

When using C++ STL containers, under what conditions must reference values be accessed? For
I'm trying to debug some STL based C++ code in gdb. The code has
C# has generic function types such as Action<T> or Func<T,U,V,...> With the advent of
Just idly curious why the compare function for stl::sort can't be a static member?
Hey there! I'm doing this project and right now I'm trying to: create some
I know the STL has set_difference , but I need to just know if
Which STL container has a thread safe insertion process? I want several threads to
I have a strongly recursive function, that creates a (very small) std::multimap locally for
I have a class A which has an reference to an object of class
I'm writing my own implementation of the C++ STL map container. Now I'm trying

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.