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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:13:17+00:00 2026-05-14T06:13:17+00:00

I have the following code: void foo() { vector<double> v(100,1); // line 1 //

  • 0

I have the following code:

void foo()
{
    vector<double> v(100,1);       // line 1
    // some code
    v = vector<double>(200,2);     // line 2
    // some code
}

what happened to the vector of size 100 after the second line? Is it gets cleared by itself? If the answer is yes, how and when it is cleared?

By the way, is there any other “easy and clean” ways to change the vector as in line 2? I don’t want things like

v.resize(200);
for (int i=0; i<200; i++) v[i] = 2;

Another question. What if the vector is a member of a class:

class A{
public:
    vector<double> data;

    A(int size, double value)
    {
        data = vector<double>(size,value);
    }
};

A object(10,1);
// some code
object = A(20,2);     // ** What happened to the old object.data? **
// some 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-14T06:13:18+00:00Added an answer on May 14, 2026 at 6:13 am

    In the assignment, first a temporary vector object is created that contains 200 times the element 2. Then this temporary object is assigned to v: The vector v removes all the elements it currently contains and copies the contents of the temporary vector object. At the end of the statement the temporary vector object is destroyed again.

    To do this more concisely without creating a temporary object you can use the assign() method:

    v.assign(200, 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: void Foo() { static std::vector<int>(3); // Vector object is
Lets say I have the following code. double *return_array(void) { double foo[2]; foo[0] =
I have the following code, which does some iterator arithmetic: template<class Iterator> void Foo(Iterator
I have the following code snippet: void foo(double a) {} namespace bar_space { struct
I have the following code: [TestMethod] public void TestFoo() { Foo(null); } private void
If I have the following code: public class Foo { public void Bar() {
I have the following code: private String foo; public void setFoo(String bar) { foo
I have the following code: #include <cstdio> template<class Fun, class... Args> void foo(Fun f,
I have the following code: public void someMethod() { Set<Foo> fooSet = bar.getFoos(); for(Foo
I have the following code: template<int k> void foo() { } int main(int argc,

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.