![]() |
In C++, vectors are dynamic containers with the ability to resize themselves automatically when an element is inserted or deleted. In this article, we will learn how to find the last element in a vector that satisfies the given condition in C++. Example Input: myVector ={10, 20, 40, 20, 50, 10, 20, 30} Output: Element present at index: 6 Find the Last Element in Vector Which Satisfies a ConditionTo find the last element in a vector that satisfies the given condition, we can use the std::find_if() method provided by the STL library in C++. This method will return the iterator to the matching element. We then use the std::distance() to calculate the actual index value. C++ Program to Find the Last Element in Vector Which Satisfies a ConditionIn this example, we will find the last even number in the vector container as an example. C++
Output
The last even number in the vector is at: 7 Time Complexity: O(N), where N is the total number of elements present in the vector. |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |