Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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
Comment

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];
Comment

c++ access second last element of vector

arr2.rbegin()[1] // rbegin() is reverse order starting at 0 for last element, 1 for second-last
Comment

c++ get last element in vector

std::v.back()
Comment

c++ last element of vector

int var = vec.back().c;
Comment

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]);
Comment

PREVIOUS NEXT
Code Example
Cpp :: initialize vector of vector c++ 
Cpp :: concat two vectors c++ 
Cpp :: hello world in c++ 
Cpp :: sort vector struct c++ 
Cpp :: c++ lambda 
Cpp :: how to input a vector when size is unknown 
Cpp :: C++ break with for loop 
Cpp :: draw rectangle opencv c++ 
Cpp :: cpp create lambda with recursion 
Cpp :: how to search in array c++ 
Cpp :: union of two arrays leetcode 
Cpp :: selection sort c++ algorithm 
Cpp :: pragma cpp 
Cpp :: c++ vs g++ 
Cpp :: remove element from vector c++ 
Cpp :: C++ float and double Different Precisions For Different Variables 
Cpp :: cpp while 
Cpp :: descending order c++ 
Cpp :: what is meant by pragma once in c++ 
Cpp :: how to print in new lines in C++ 
Cpp :: inline c++ 
Cpp :: attention nlp 
Cpp :: how to create a file in c++ 
Cpp :: read string with spaces in c++ 
Cpp :: c++ vector first element 
Cpp :: take a function as an argument in c++ 
Cpp :: how to empty a std vector 
Cpp :: max pooling in c++ 
Cpp :: pop off end of string c++ 
Cpp :: Operators in C / C++ 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =