How to find index of a given element in a Vector in C++

Опубликовано: 22 Октябрь 2024
на канале: Quick Fix “Quick Fix”
2,967
22

In this video I have told how you can find index of an element in vector using iterator.

Time complexity of std::find function is O(n) where n is length of vector.

Prototype of find function is:
std::find(starting_iterator, end_iterator, element to be searched);

Approach:
Follow the steps below to solve the problem:

1. find(): Used to find the position of element in the vector.
2. Subtract from the iterator returned from the find function, the base iterator of the vector .
3. Finally return the index returned by the subtraction.

If you have any query write it down in the comment section.
Please like, comment and share if you find this video useful.