.back() in c++ code example
Example 1: vector last c++
// vector::back
int main ()
{
std::vector<int> myvector;
myvector.push_back(10);
while (myvector.back() != 0)
{
myvector.push_back ( myvector.back() -1 );
}
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size() ; i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
}
Example 2: back() in c++
// my linkedin : https://www.linkedin.com/in/vaalarivan-prasanna-3a07bb203/
vector<int> vec = {1, 2, 5, 6};
int ele = vec.back(); //ele = 6
//back() method does delete the last element from the vector, it just returns\n
//the last element.
Example 3: c++ vector.back
int main()
{
std::vector<int> myvector;
//add 2 to the back
myvector.push_back(2);
std::cout << myvector.back() << std::endl; //this will print 2
myvector.push_back(46);
std::cout << myvector.back() << std::endl; //prints 46
return 0;
}
/*Output
2
46
*/