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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:18:47+00:00 2026-05-31T12:18:47+00:00

I am looking for the way to initialize a vector with the minimal amount

  • 0

I am looking for the way to initialize a vector with the minimal amount of copy.

struct T {
   std::vector<int> v;
   //some stuff here ; pod
   T(std::vector<int> vv):v(vv){}://a non default constructor
};

Now to initialize a vector, i do

std::vector<T> vec(n);
for (auto it = vec.begin() ;it != vec.end(),++it) 
{
  // do some stuff to *it;
}

the “do some stuff here” basically just redo what is done in the constructor, and so i have two initialization of T.v…
I also thought of doing

std::vector<T> vec;
for (int i = 0 ; i< n;++i) 
{
   std::vector<int> vv = // blabla
   T t(vv);
   vec.push_back(t);
}

which again result on vv being copied…

So how do i initialize a vector of struct, where each element is initialized using a non default constructor (call with different argument for each element) ?

  • 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-31T12:18:48+00:00Added an answer on May 31, 2026 at 12:18 pm

    Use reserve and construct elements in place:

    std::vector<T> vec;
    vec.reserve(n);     // now guaranteed O(1) insertions at the end
    
    for (unsigned i = 0; i != n; ++i)
    {
       vec.emplace_back(vv);  // or whatever
    }
    

    You can also use the old-style push_back(); with any luck, the compiler will optimize out several unneeded copies. You should probably also add an iterator-based and an initializer-list constructor to T, just to give you some flexibility.

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

Sidebar

Related Questions

I'm looking for the best way to initialize a knockout observable array from some
I was looking for a way to create a nice progressbar. I found some
What's the best way to lazily initialize a collection, I'm specifically looking at Java.
Possible Duplicate: C++: Easiest way to initialize an STL vector with hardcoded elements I'm
I'm looking for a method for initializing a complex struct which contains vector in
I'm looking for way to PHP to detect if a script was run from
I'm looking a way to enable IP logging with log4net in ASP.NET. I found
I'm looking for way to write Javascript programs / scripts on desktop, not inside
I'm looking a way to build conditional assignments in bash: In Java it looks
I am using codeigniter and looking a way to enable directly editting of doc

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.