Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can vector.back() be used to assign a value to the last element of a vector?

Tags:

c++

vector

I have some code that needs to assign the first and last elements of a vector as a special case.

Is somevector.back() an lvalue? Will the following code work?

some_vector.front()=first_value;
some_vector.back()=last_value;
like image 277
Dan Avatar asked Dec 13 '12 01:12

Dan


1 Answers

Yes it can, as it returns a reference to the last element. Better make sure it's not empty first!

like image 108
goji Avatar answered Oct 22 '22 13:10

goji