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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:54:30+00:00 2026-06-12T21:54:30+00:00

I have a std::vector. I assign this vector like vec.assign(20000, 0); Now I have

  • 0

I have a std::vector. I assign this vector like

vec.assign(20000, 0);

Now I have an additional array, which I want to insert into the vector. But NOT just like insert, more replace elements in the vector.

uint8_t a[] = {1,2,3,4,5,6,7,8};

Move that array into the vector on position x – x+8 without allocating new memory.

What I tried and works is of course

vec.insert(start, a, a+8);

But there I am allocating new memory and the size of the vector changes, which is not supposed to happen.
Yes, I could delete, the entries, which are too much, but there’s still the problem, that I am allocating more memory. Isn’t there a possibility, to just replace the array with the contents of the vector? Smth like that:

vec.replace(start, a, a+8);

I wanted to avoid replacing each element, cause I am afraid that could take too long.

What do you think?
Is there a way to do that?
Have you had that problem before, too? How did you fix it?

  • 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-12T21:54:31+00:00Added an answer on June 12, 2026 at 9:54 pm

    Simple

    #include <algorithm>
    
    std::copy(a, a + 8, vec.begin());
    

    Copies elements from a upto a + 8 and replaces the elements starting at vec.begin().

    I think you are wrong to worry about efficiency. The solution above does replace each element, and would likely be no more nor no less efficient than if you wrote the replacement code yourself. If the code above has any advantage it is clarity, a much overlooked advantage.

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

Sidebar

Related Questions

Usually like this: #include <boost/assign/std/vector.hpp> vector<int> v; v += 1,2,3,4,5; Except for a: #include
I have a std::vector<int> , and I want to delete the n th element.
I have a std::vector. I want to create iterators representing a slice of that
I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have an std::vector<unsigned char> with binary data in. I just would like to
I have std::vector<int> around , in which I need to update(/overwrite) N values starting
I have a library which expects a array and fills it. I would like
Possible Duplicate: sum of elements in a std::vector I have std::vector<int> and I want
I have a question regarding vectors: If I have a std::vector<MyClass> will this vector
I have several std::vector , all of the same length. I want to sort

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.