Search
 
SCRIPT & CODE EXAMPLE
 

CPP

find last element of an array c++

int arr={1,2,3,4,5,6};
int length=sizeof(arr)/sizeof(int);
int lastElement=aar[length-1];
Comment

c++ last element of array

int arr[10];
int len = sizeof(arr) / sizeof(arr[0]);
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

How to get the last element of an array in C++ using std::array

#include <array>
std::array<int, 5> a {1, 2, 3, 4, 5};
int i = a[a.size() - 1]; // The last variable stored in i
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ pointers 
Cpp :: << in C++ 
Cpp :: c++ main function parameters 
Cpp :: even and odd numbers 1 to 100 
Cpp :: min heap 
Cpp :: virtual function in c++ 
Cpp :: pause the console c++ 
Cpp :: binary to decimal online converter 
Cpp :: substring function in c++ 
Cpp :: how to show constellations in starry night orion special edition 
Cpp :: cpp algorithm iota 
Cpp :: c ++ The output should be (abc),(def),(ghw) 
Cpp :: pointer to value of others file cpp 
Cpp :: error: use of parameter outside function body before ] token c++ 
Cpp :: c++ friend keyword 
Cpp :: c++ online 
Cpp :: cplusplusbtutotrail 
Cpp :: How to remove the % in zsh that show after running c++ file 
Cpp :: c++ execute thread and forget 
Cpp :: decemal representation 
Cpp :: argument to number C++ 
Cpp :: is variable sized array are not allowed in c++? 
Cpp :: c++ program to convert celsius to fahrenheit 
Cpp :: contains in c++ map 
Cpp :: C++ initializing a thread with a public function of a class 
Cpp :: sort using comparator anonymous function c++ 
Cpp :: for llop in c++ 
Cpp :: how to find the left most bit 1 in binary of any number 
Cpp :: export gcc g++ 
Cpp :: how to insert variable into string c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =