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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:50:51+00:00 2026-06-04T12:50:51+00:00

Is there a common idiom for doing something twice, as in the following situation?

  • 0

Is there a common idiom for doing something twice, as in the following situation?

    for ( int i = 0; i < num_pairs; i++ ) {
        cards.push_back( Card(i) );
        cards.push_back( Card(i) );
    }

I have a feeling that there’s a clearer way than introducing a new loop variable counting from 0 to 1, especially since it isn’t used except for counting.

    for ( int i = 0; i < num_pairs; i++ )
        for ( int j = 0; j < 2; j++ )
            cards.push_back( Card(i) );

(Card is just some class I made up and not relevant to the question.)

  • 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-04T12:50:52+00:00Added an answer on June 4, 2026 at 12:50 pm

    I’ve got a few suggestions. See the last for my recommendation:

    1. In my opinion insert(it, N, value) beats std::fill_n:

      for ( int i = 0; i < num_pairs; i++ ) {
          cards.insert(cards.end(), 2, Card(i) );
      }
      
    2. If order isn’t important, you could just generate the cards once, and duplicate after the fact

      std::copy(cards.begin(), cards.end(), std::back_inserter(cards));
      
    3. Using a dirty lambda and integer division trick. Warning this has the hallmark of a premature optimization: reduces readability for no good reason.

      std::vector<Card> cards(num_pairs * 2);
      int i = 0;
      std::generate_n(cards.begin(), num_pairs, [&i] () { return Card(i++/2); });
      

      (assumes Card is default-constructible. If not, use cards.back_inserter())

    4. My recommendation

      The following wins in both performance and expression of intent:

      std::vector<Card> cards;
      cards.reserve(num_pairs*2);
      for (int i = 0; i < num_pairs; ++i)
      {
          cards.emplace_back(i);
          cards.emplace_back(i);
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am pretty sure there is a common idiom, but I couldn't find it
in Java the following is a common idiom: if( null != obj && obj.getSomeNumber()
Is there a common Javascript/Coffeescript-specific idiom I can use to accomplish this? Mainly out
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
The iPhone platform has a number of common gesture idioms. For example, there are
Is there a common standard for reporting errors in your class functions? I've seen
Is there a common interface in Python that I could derive from to modify
Is there a common method/api to list all web browsers (name, executable, default yes/no)
Is there a common way of counting source lines of code (SLOC) in a
Evening, Is there a common or standardised name given to a function or method

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.