Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing Vectors in C++

Tags:

c++

c++11

vector

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.

like image 386
Smithy Avatar asked Dec 16 '22 05:12

Smithy


1 Answers

"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>.

like image 82
elnigno Avatar answered Dec 25 '22 03:12

elnigno