Smithy
Smithy

Reputation: 1207

Constructing Vectors in C++

I came across this code recently but don't quite understand what's going on.

auto c = vector<int> {};

What is the vector constructor returning?

Then this code:

c = vector<int> {1,2,3,4,5 };

Is the second c at a different memory location to the initial c?

Is the destructor called when c is reinitialised?

I searched the internet but could not find any examples of the above code.

How is the above different to

vector<int> c {};

Thanks in advance for any help.

Upvotes: 4

Views: 405

Answers (5)

codeling
codeling

Reputation: 11448

What is the vector constructor returning?

An empty vector<int>; and the compiler deduces the type of the variable c from that constructor call, meaning c will get the type vector<int>. auto basically just saves you from typing the variable type two times - it's given already in the constructor call anyway, you now don't have to type it in front of the variable name a second time, you can use auto instead.

Is the second c at a different memory location to the initial c?

No, it is the same vector; but the values from another, temporary vector<int> (which holds the values 1,2,3,4 and 5) get assigned to c via operator=. That means, the address of c itself (&c) won't be changed. However, the data it contains (e.g. the result of c.data()) can and probably will change.

Is the destructor called when c is reinitialised?

Not c's destructor. Only the one from the temporary.

Upvotes: 3

rnjai
rnjai

Reputation: 1135

C++11 includes the auto keyword which does Type Inference for you.
It helps to simplify code by a great deal.
Example:

auto itr = vec.iterator(); // instead of vector<int>::iterator itr

Upvotes: 1

Duncan Smith
Duncan Smith

Reputation: 538

The 'first c' is where the variable c (a vector of int) is defined.

auto c = vector<int> {};

The 'second c' is just re-assignment of the value of c. It is not a new variable so the memory address of c does not change and the destructor of c is not called.

c = vector<int> {1,2,3,4,5 };

What actually happens is that vector {1,2,3,4,5 } creates a temporary object of type vector which is initialized from the initializer list with the values 1,2,3,4,5. This temporary object is then passed to c's copy constructor (or in C++11) the move constructor, so that c replaces its current values (if any) with the values from the temporary object.

The destructor for c will not be called until it goes out of scope (for e.g. the function exits or the control block {} in which it was defined exits).

Upvotes: 2

Matthias247
Matthias247

Reputation: 10436

The first and the last line are functionally equivalent.

For the assignment:

c = vector<int> {1,2,3,4,5 };

c will not be destructed or be afterwards located at a new memory operation. What happens is that an unnamed second vector will be created with the 5 values, and the vector::operator= will be used to assign the content of that vector to c. This will happen in term of a move operation in C++11. Afterwards the temporary vector will be destroyed and it's destructor called, but not the destructor of c.

Upvotes: 1

elnigno
elnigno

Reputation: 1821

"As jrd1 says, it's a C++11 feature.

The keyword auto basically means that you let the compiler "guess" the type of the variable.

So c is a regular vector<int>.

Upvotes: 5

Related Questions