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

PREVIOUS NEXT
Code Example
Cpp :: getline cin is being skipped 
Cpp :: calculate how many liters would be needed 
Cpp :: custom comparator in set of struct 
Cpp :: c++ display numbers as binary 
Cpp :: min heap in c++ 
Cpp :: c++ program to add two numbers using function 
Cpp :: how to remove spaces from a string 
Cpp :: how to writt array in c++ 
Cpp :: C++ shortcuts in desktopp app 
Cpp :: prime number program 
Cpp :: c++ main environment variables 
Cpp :: c++ virtual function in constructor 
Cpp :: unclebigbay 
Cpp :: how to read a line from the console in c++ 
Cpp :: how to initialize 2d vector in c++ 
Cpp :: c++ round number up 
Cpp :: heap buffer overflow c++ 
Cpp :: c++ string remove last character 
Cpp :: combination code c++ 
Cpp :: opencv rgb to gray c++ 
Cpp :: input 2d vector c++ 
Cpp :: c++ reverse integer 
Cpp :: random number cpp 
Cpp :: convert all characters in string to uppercase c++ 
Cpp :: what is the associative property of an operator 
Cpp :: c++ function 
Cpp :: c++ sieve of eratosthenes 
Cpp :: Story of c++ 
Cpp :: vector size for loop 
Cpp :: time_t to int 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =