61.4k views
4 votes
Which assigns the last vector element with 20? vector int> user Num(N_SIZE); o userNum.at(20); o userNum.at() = 20; o userNum.at(N_SIZE = 20; o userNum.at(N_SIZE - 1) = 20;

1 Answer

0 votes
The correct way to assign the last element of a vector of integers named userNum with the value 20 would be:

userNum.at(userNum.size() - 1) = 20;

Step-by-step explanation:

userNum.at(userNum.size() - 1) is used to access the last element of the vector userNum. userNum.size() returns the number of elements in the vector, and subtracting 1 gives the index of the last element since vector indices are zero-based.
= is the assignment operator used to assign the value 20 to the last element of the vector.
So, the correct statement would be userNum.at(userNum.size() - 1) = 20;.
User Vincent Poirier
by
7.8k points