Answers for "back instert vector c+++"

C++
3

c++ vector.back

#include <iostream>
#include <vector>

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
*/
Posted by: Guest on August-14-2020
0

back_inserter in vector c++

// back_inserter example
#include <iostream>     // std::cout
#include <iterator>     // std::back_inserter
#include <vector>       // std::vector
#include <algorithm>    // std::copy

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;
}
Posted by: Guest on August-25-2020

Browse Popular Code Answers by Language