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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:51:48+00:00 2026-05-25T22:51:48+00:00

Presenting the minimal code to describe the problem: struct A { vector<string> v; //

  • 0

Presenting the minimal code to describe the problem:

struct A {
  vector<string> v;
  // ... other data and methods
};
A obj;
ifstream file("some_file.txt");
char buffer[BIG_SIZE];
while( <big loop> ) {
  file.getline(buffer, BIG_SIZE-1);
  // process buffer; which may change its size
  obj.v.push_back(buffer);  // <------- can be optimized ??
}
...

Here 2 times string creation happens; 1st time to create the actual string object and 2nd time while copy constructing it for the vector. Demo

The push_back() operation happens millions of times and I am paying for one extra allocation those many times which is of no use for me.

Is there a way to optimize this ? I am open for any suitable change. (not categorizing this as premature optimization because push_back() happens so many times throughout the code).

  • 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-25T22:51:48+00:00Added an answer on May 25, 2026 at 10:51 pm

    Well, you get two allocations, but not both of them are of the string: one of them creates the string, while the other creates just a pointer inside of the vector (note that this depends on the compiler: some compilers/settings might indeed create two strings, but most won’t). Look at this code for the demo.

    One way to optimize it would be using the char* instead of the string as the template parameter (don’t forget to manually delete it before killing the vector!). This way you’ll get rid of one (biggest) of the allocations. Alternatively, just use your own implementation of vector: you’ll be able to control every aspect of memory allocation then.

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

Sidebar

Related Questions

I'm working on a site where I use Highcharts quite heavily for presenting data
I have the following code that is presenting a 'word-of-the-day', As I am relatively
Background I am presenting data using a HTML frameset. The left-side frame is a
When presenting preformatted text on the web (e.g. code samples), line wrapping can be
I'm presenting data in a matrix (a crosstab). To ensure that all rows are
I am presenting a TTWebController as a Modal View using this code: TTNavigator* navigator
I am using the following code for presenting some measurements, their estimates and their
I'm presenting data for users in a grid (ExtJS) with remote sorting and paging.
I have a webgrid presenting data grouped by weeks. The first grid column contains
I recently faced a problem of presenting the output of simple genetic algorithm that

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.