Answers for "vector last c++"

C++
18

access last element in vector in c++

vector<int> v;
cout << v[v.size() - 1];
cout << *(v.end() - 1);
cout << *v.rbegin();
// all three of them work
Posted by: Guest on May-31-2020
7

vector last c++

// vector::back
#include <iostream>
#include <vector>

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;
}
Posted by: Guest on June-04-2020
1

c++ get last element in vector

std::v.back()
Posted by: Guest on May-30-2020

Browse Popular Code Answers by Language