Horje
cpp get last element of vector Code Example
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
c++ get last element in array
#include<iostream>
/*To get the last element of the array we first get the size 
    of the array by using sizeof().  Unfortunately, this gives 
    us the size of the array in bytes.  To fix this, we divide
    the size (in bytes) by the size of the data type in the array.
    In our case, this would be int, so we divide sizeof(array) 
    by sizeof(int).  Since arrays  start from 0 and not 1 we 
    subtract one to get the last element.
    -yegor*/
int array[5] = { 1, 2, 3, 4, 5 };
printf("Last Element of Array: %d", array[(sizeof(array)/sizeof(int))-1]);
cpp get last element of vector
vector<int> vec;
vec.push_back(0);
vec.push_back(1);
int last_element = vec.back();
int also_last_element = vec[vec.size() - 1];
c++ access second last element of vector
arr2.rbegin()[1] // rbegin() is reverse order starting at 0 for last element, 1 for second-last
c++ last element of vector
int var = vec.back().c;




Cpp

Related
string split by space c++ Code Example string split by space c++ Code Example
cmake define standard c++ Code Example cmake define standard c++ Code Example
New Year's Eve Code Example New Year's Eve Code Example
binary search c++ Code Example binary search c++ Code Example
c++ accessing parent class functinons Code Example c++ accessing parent class functinons Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8