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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:45:48+00:00 2026-06-03T02:45:48+00:00

How does a std::vector<std::string> initialize its self when the following code is invoked std::vector<std::string>

  • 0

How does a std::vector<std::string> initialize its self when the following code is invoked

std::vector<std::string> original;
std::vector<std::string> newVector = original;

It would seem as if the copy constructor would be invoked on std::vector<std::string> new during newVector = original, but how are the std::string‘s brought over inside of the orginal? Are they copies or new std::string‘s? So is the memory in newVector[0] the same as original[0].

The reason I ask is say I do the following

#include <vector>
#include <string>
using namespace std;

vector<string> globalVector;

void Initialize() {
    globalVector.push_back("One");
    globalVector.push_back("Two");
}

void DoStuff() {
    vector<string> t = globalVector;
}

int main(void) {
    Initialize();
    DoStuff();
}

t will fall out of scope of DoStuff (on a non optimized build), but if it t is just filled with pointers to the std::string‘s in globalVector, might the destructor be called and the memory used in std::string deleted, there for making globalVector[0] filled with garbage std::string‘s after DoStuff is called?

A nut shell, I am basically asking, when std::vector‘s copy constructor is called, how are the elements inside copied?

  • 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-03T02:45:49+00:00Added an answer on June 3, 2026 at 2:45 am

    std::vector and most other standard library containers store elements by value. The elements are copied on insertion or when the container is copied. std::string also maintains its own copy of the data, as far as your usage of it is concerned.

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

Sidebar

Related Questions

If std::vector and friends are self resizing, does that mean if I declare a
Is this code valid? int foo() { std::vector<std::string>& v = std::vector<std::string>(5, X); // Do
When I call std::vector::reserve when the identifier is of type std::vector<Foo*> reserve(...) does nothing:
Why does the following not work: #include <iostream> #include <fstream> #include <stack> std::stack<std::ifstream> s;
std::string x(x); This crashes very badly on my compiler. Does this mean I should
I'm porting some C++ code to C#. Does C# have an equivalent to std::nth_element()
It seems that I can sort a std::vector<std::pair<int, std::string>> , and it will sort
The visual studio std::vector visualizer in the VS2008 autoexp.dat file doesn't seem to work
A problem of value types with external resources (like std::vector<T> or std::string ) is
Possible Duplicate: Converting std::string to std::vector<char> I tried: std::string str = hello; std::vector<char> data;

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.