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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:53:08+00:00 2026-06-13T02:53:08+00:00

I need a vector with n elements inside(I mean the size not the capacity

  • 0

I need a vector with n elements inside(I mean the size not the capacity of the vector is n) but don’t want to initialize any of them during creation because that’s nothing but rubbish to me. Is there any efficient way to achieve this?

Edit: All I can imagine is to use reserve(n) and do some hack(can somebody tell me how) to make the vector believe it has n elements. Or I just jump into a cesspit and dance with malloc and free.

  • 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-13T02:53:09+00:00Added an answer on June 13, 2026 at 2:53 am

    When you construct a vector with the constructor taking a size parameter (and don’t specify an element to copy into the vector), the elements will be value-initialized. The rules for value initialization (8.5p7) specify that primitives are zero-initialized.

    The only way round this is to wrap your primitive in a class type that defines a no-op default constructor:

    template<typename T>
    struct A {
        A() {};
        T value;
    };
    
    std::vector<A<int>> vec(1000);
    

    If you can encapsulate the initialization process as an iterator, you could use the iterator to construct the vector:

    std::vector<int> vec(it_begin, it_end);
    

    Otherwise I’d recommend not worrying too much about the overhead of initialization; it’s not going to change the complexity of your algorithm (since you’ll be accessing each member anyway, which makes it O(n) already).

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

Sidebar

Related Questions

I need to have a fixed-size array of elements and to call on them
I need to store a huge number of elements in a std::vector (more that
I need to take a C++ vector with potentially a lot of elements, erase
I have a vector of booleans. I need to set its elements from n-th
I need to convert a string vector in a simple string. I do not
I have a vector containing n elements. I need to choose a subset of
I want to get a vector of elements of a Matlab matrix at predefined
I need an algorithm or a standard library function for comparing two vector elements,
I need to store some names in Vector and then I want to convert
I have a std::vector with n elements. Now I need to pass a pointer

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.