1.

Which of the following(s) can be used to access the last element of a vector v?(a) v.end()(b) v.cend()(c) both v.end() and v.cend()(d) vectors do not have a function to access the last elementThe question was posed to me during an internship interview.I want to ask this question from seq_con Vector Class topic in portion Class Hierarchies, Library & Containers of C++

Answer»

Right answer is (d) vectors do not have a function to access the last element

The EXPLANATION: There are no function to access the last element of the VECTOR. The end() and CEND() returns the ITERATOR to an element which is kept at the last of the vector to keep the knowledge about the end of a vector. In order to access the last element, you can first find the size and then can use V[size-1] or v.at(size – 1) to access the last element.



Discussion

No Comment Found

Related InterviewSolutions