back instert vector c+++ code example
Example 1: c++ vector.back
#include <iostream>
#include <vector>
int main()
{
std::vector<int> myvector;
myvector.push_back(2);
std::cout << myvector.back() << std::endl;
myvector.push_back(46);
std::cout << myvector.back() << std::endl;
return 0;
}
Example 2: back_inserter in vector c++
#include <iostream>
#include <iterator>
#include <vector>
#include <algorithm>
int main () {
std::vector<int> foo,bar;
for (int i=1; i<=5; i++)
{ foo.push_back(i); bar.push_back(i*10); }
std::copy (bar.begin(),bar.end(),back_inserter(foo));
std::cout << "foo contains:";
for ( std::vector<int>::iterator it = foo.begin(); it!= foo.end(); ++it )
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}