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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:13:19+00:00 2026-06-18T11:13:19+00:00

Let’s say I have a vector<string> containing a and b, I wanna copy itself

  • 0

Let’s say I have a vector<string> containing “a” and “b”, I wanna copy itself 2 times so that the vector now contains “a”, “b”, “a”, “b”, “a”, “b”

What is a better approach than using for and push_back?

  • 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-18T11:13:20+00:00Added an answer on June 18, 2026 at 11:13 am

    My initial thought:

    myvec.reserve(myvec.size()*3);  //reserve not only speeds things upt it also protects us ftom iterator invalidation
    vector<string>::iterator it = myvec.end();    //we ant to add two times the origional onto the end so we save the end of the origional end
    myvec.insert(myvec.end(), myvec.begin(), it);
    myvec.insert(myvec.end(), myvec.begin(), it);
    

    thanks to Emilio Garavaglia for first pointing out problems with this, see here many reasons why this has problems: Does std::vector::insert() invalidate iterators if the vector has enough room (created through reserve)?

    Second try:

    std::size_t size = myvec.size();
    myvec.resize(size*3);  //resize must protects us from iterator invalidation
    vector<string>::iterator it = myvec.begin()+size;
    std::copy(myvec.begin(),it,it);
    std::copy(myvec.begin(),it,it+size);
    

    since no implementation is going to implement a std::string whos default constructor allocates something on the heap this should cause less heap access and therefore be faster than others examples.

    Another heap access minimization is to copy the vector into another insert it and then move in the originals, I stole Emilio Garavaglia code and pimped it:

    {
    vector<string> m = { "a", "b" };
    auto n = m; // keep initial value safe
    m.reserve(3 * m.size()); // preallocate, to avoid consecutive allocations
    m.insert(m.end, n.begin(), n.end());
    std::for_each(n.begin(),n.end(),[&n](std::string &in){n.emplace_back(std::move(in));});
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say for a moment that I have the following module in python: class
Let's say I have a posts table that has many comments (and a comment
Let's say I have the function #include <string> std::string const foo() { std::string s
Let's say you have a class library project that has any number of supplemental
Let's say I have an abstract parent class called shape, and that there are
let's say.. I have the following java bean. Case1: (Student Bean) Integer id; String
Let say I have my view that I use it as a toggle button.

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.