Access last element
Returns a reference to the last element in the
vector.
Unlike member vector::end, which returns an iterator just past this element, this function returns a direct reference.
Calling this function on an empty container causes undefined behavior.
back函数:
reference back();const_reference back();
功能:
返回当前vector容器中末尾元素的引用。
不同于vector::end一样,会返回一个迭代器中过去的元素,这个函数返回一个直接引用。
// vector::back
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> myvector;
myvector.push_back(10);
while (myvector.back() != 0)
{
myvector.push_back ( myvector.back() -1 );
}
cout << "myvector contains:";
for (unsigned i=0; i<myvector.size() ; i++)
cout << ' ' << myvector[i];
cout << '\n';
return 0;
}