这不起作用 - 添加(或删除)元素std::vector会使所有迭代器无效到该向量中.要获得最后一个元素的迭代器,可以使用减法:
std::vector<T> vec;
vec.push_back(something); //doing this so that the vector is not empty and the decrement is valid
auto itToLast = vec.end() - 1;
Run Code Online (Sandbox Code Playgroud)
根据你需要做的事情,你可能还会考虑rbegin(),它为最后一个元素提供了一个反向迭代器(递增这样的迭代器将它移动到最后一个,依此类推).
最后,减法仅适用于随机访问迭代器(std::vector提供那些,但是例如std::list不提供).对于其他迭代器,您可以使用std::advance或std::prev.