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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:48:15+00:00 2026-06-15T23:48:15+00:00

I have an array, say ARR, and the total number of objects in ARR

  • 0

I have an array, say ARR, and the total number of objects in ARR is known.
I want to copy (put) an attribute of each object in ARR into an STL vector, say VEC.

One way is to iterate through ARR

VEC.pushback(ARR[i].att);

The other way is

VEC.resize(ARR.size());
VEC[i] = ARR[i].att;

Do them make difference regarding runtime performance? Which one is better? Thanks.

FOLLOW-UP: I would like to provide some experiment results for anyone interested.
I tried (1) resize() + ‘=’ and (2) reserve() + push_back() methods by putting 50000 integers into an STL vector.

(1) takes 0.000201s;
(2) takes 0.000229s.
  • Both were compiled with g++ -O3, and I ran the program several times.
  • (1) outperforms (2) consistently.
  • (1) has extra allocations, thus taking slightly more memory in terms of space.
  • 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-15T23:48:15+00:00Added an answer on June 15, 2026 at 11:48 pm

    Calling resize() (or reserve()) before may potentially save some resizing as you add new items. Anyway, you already know the number of items, so it makes even more sense.

    With resize() you will have a default construct step, I’d go with reserve() to prevent reallocations and push_back() (and in C++11, emplace_back() if your compiler supports it)

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

Sidebar

Related Questions

Lets say I have an array like int arr[10][10]; Now i want to initialize
Lets say I have an array arr of objects of type A in memory,
I have an array of values say arr(0)=Sam arr(1)=1452 arr(2)=NY I want to generate
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Let's say I have a 10 element array: from ctypes import * arr =
I have an array of ActiveRecord objects, each one which has its own respective
Say I have an array of integers: arr = [0,5,7,8,11,16] and I have another
Let's say I have the following array: arr = [, 2121, 8, myString] I
Lets say i want to resize an array of int pointers, i have a
Let's say I have an array arr . When would the following not give

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.