33.2k views
4 votes
Assume you define a vector in the following way:

vector vec;

Assign the value 10 to the first element of this vector. What is the statement you would use?

1 Answer

6 votes

Answer:

vec[0].push_back(10)

Step-by-step explanation:

Given

Declaration: vector <int> vec

Required

Assign 10 to the first element

This can be done using the push_back keyword.

The syntax is: vectorname[position].push_back(value);

In this case:

vectorname = vec

position = 0 i.e. first element

value = 10

So, we have:

vec[0].push_back(10)

User Burk
by
6.5k points