Get first and last elements from Array and Vector in CPP Last Updated : 10 Nov, 2018 Comments Improve Suggest changes Like Article Like Report Given an array, find first and last elements of it. Input: {4, 5, 7, 13, 25, 65, 98} Output: First element: 4 Last element: 98 In C++, we can use sizeof operator to find number of elements in an array. CPP // C++ Program to print first and last element in an array #include <iostream> using namespace std; int main() { int arr[] = { 4, 5, 7, 13, 25, 65, 98 }; int f, l, n; n = sizeof(arr) / sizeof(arr[0]); f = arr[0]; l = arr[n - 1]; cout << "First element: " << f << endl; cout << "Last element: " << l << endl; return 0; } Output: First element: 4 Last element: 98 We should not sizeof when arrays are passed as parameters, there we must pass size and use that size to find first and last elements. CPP // C++ Program to print first and last element in an array #include <iostream> using namespace std; int printFirstLast(int arr[], int n) { int f = arr[0]; int l = arr[n - 1]; cout << "First element: " << f << endl; cout << "Last element: " << l << endl; } int main() { int arr[] = { 4, 5, 7, 13, 25, 65, 98 }; int n = sizeof(arr) / sizeof(arr[0]); printFirstLast(arr, n); return 0; } Output: First element: 4 Last element: 98 In case of vectors in C++, there are functions like front and back to find first and last elements. CPP // C++ Program to find first and last elements in vector #include <iostream> #include <vector> using namespace std; int main() { vector<int> v; v.push_back(4); v.push_back(5); v.push_back(7); v.push_back(13); v.push_back(25); v.push_back(65); v.push_back(98); cout << "v.front() is now " << v.front() << '\n'; cout << "v.back() is now " << v.back() << '\n'; return 0; } Output: v.front() is now 4 v.back() is now 98 We can use front and back even when vector is passed as a parameter. Comment More infoAdvertise with us Next Article Get first and last elements from Array and Vector in CPP aishwarya.27 Follow Improve Article Tags : Misc C++ STL cpp-vector Practice Tags : CPPMiscSTL Similar Reads How to copy elements of an Array in a Vector in C++ An Array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. Vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted, with their storage being handled 6 min read Find the Frequency of Each Element in a Vector in C++ The frequency of an element is number of times it occurred in the vector. In this article, we will learn different methods to find the frequency of each element in a vector in C++.The simplest method to find the frequency of each element in a vector is by iterating the vector and storing the count o 3 min read How to delete last element from a map in C++ If we wish to delete the last element from a map, we can use following methods : using prev(mp.end()) : what prev function do is, it goes back one step back from the given iterator. So using prev function with mp.end() will return an iterator which points the last element of the map. Implementation: 2 min read Reading Lines by Lines From a File to a Vector in C++ STL Prerequisites: STL in C++Vector in C++File handling in C++ The Standard Template Library (STL) is a set of C++ template classes to provide common programming data structures and functions such as lists, stacks, arrays, etc. It is a library of container classes, algorithms, and iterators. Vector in C 2 min read array::front() and array::back() in C++ STL Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::front() This function is used to reference the first element of the array container. This function can be 3 min read Different Ways to Convert Vector to Array in C++ STL In C++, vectors are dynamic arrays that can resize according to the number of elements. In this article, we will learn how to convert a vector to a C-style array in C++.The easiest way to convert a vector to array is by creating a new array and copy all the elements of vector into it using copy() fu 3 min read array get() function in C++ STL The array::get() is a built-in function in C++ STL which returns a reference to the i-th element of the array container. Syntax: get(array_name) Parameters: The function accepts two mandatory parameters which are described below. i - position of an element in the array, with 0 as the position of the 2 min read Program to find the minimum (or maximum) element of an array Given an array, write functions to find the minimum and maximum elements in it. Examples:Input: arr[] = [1, 423, 6, 46, 34, 23, 13, 53, 4]Output: Minimum element of array: 1 Maximum element of array: 423Input: arr[] = [2, 4, 6, 7, 9, 8, 3, 11]Output: Minimum element of array: 2 Maximum element of ar 14 min read Vector lastElement() Method in Java The java.util.vector.lastElement() method in Java is used to retrieve or fetch the last element of the Vector. It returns the element present at the last index of the vector. Syntax: Vector.lastElement() Parameters: The method does not take any parameter. Return Value: The method returns the last el 2 min read Array of Vectors in C++ STL Prerequisite: Arrays in C++, Vector in C++ STL An array is a collection of items stored at contiguous memory locations. It is to store multiple items of the same type together. This makes it easier to get access to the elements stored in it by the position of each element. Vectors are known as dynam 3 min read Like