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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:52:29+00:00 2026-05-25T09:52:29+00:00

In the C++ Standard Template Library (STL), it is possible for example to create

  • 0

In the C++ Standard Template Library (STL), it is possible for example to create a vector consisting of multiple copies of the same element, using this constructor:

std::vector<double> v(10, 2.0);

This would create a vector of 10 doubles, initially set to 2.0.

I want to do a similar thing in C#, more specifically creating an array of n doubles with all elements initialized to the same value x.

I have come up with the following one-liner, relying on generic collections and LINQ:

double[] v = new double[n].Select(item => x).ToArray();

However, if an outsider would read this code I don’t think it would be immediately apparent what the code actually does. I am also concerned about the performance, I suppose it would be faster to initialize the array elements via a for loop (although I haven’t checked). Does anybody know of a cleaner and/or more efficient way to perform this task?

  • 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-25T09:52:30+00:00Added an answer on May 25, 2026 at 9:52 am

    What about this?

    double[] v = Enumerable.Repeat(x, n).ToArray();
    

    EDIT: I just did a small benchmark; to create 1000 arrays of 100000 elements each, using a loop is about 3 times faster that Enumerable.Repeat.

    Repeat 
    00:00:18.6875488 
    
    Loop 
    00:00:06.1628806 
    

    So if performance is critical, you should prefer the loop.

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

Sidebar

Related Questions

Currently I am studying Standard Template Library (STL). In this program I am storing
Is it possible to write the complete C++ standard library (including STL of course,
I have always been religiously using SGI's Standard Template Library Programmer's Guide (STLPG) as
I'm trying to use the C++ standard library's find algorithm like this: template<class T>
Is there a C++ Standard Template Library class that provides efficient string concatenation functionality,
I've recently started teaching myself the standard template library. I was curious as to
I just created my first MVC2 project using the standard template. I need to
Declarations of auto_ptr from C++ Standard Library namespace std { template <class Y> struct
I'm wanting to become conversant in the use of the Standard Template Library. If
I mean, aside from its name the Standard Template Library (which evolved into the

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.