How can you access the last element of a C++ STL vector named "myVector"?
- myVector.end()
- myVector.last()
- myVector.back()
- myVector[myVector.size()]
The member function back() provides direct access to the last element in a vector. On the other hand, myVector.end() returns an iterator pointing to the position after the last element, and myVector[myVector.size()] would result in undefined behavior since it tries to access out-of-bounds memory. There's no last() function in vector.
Loading...
Related Quiz
- What is the purpose of a conversion constructor in C++?
- Which operator is used to access the memory address of a variable in C++?
- In which year was the C++98 standard officially published?
- The bitwise NOT operator (~) performs a _______ operation on each bit of a number.
- How can you prevent users from mistakenly instantiating your template classes with non-numerical types in a C++ library of numerical methods, leading to confusing compiler errors?