Javatpoint Logo
Javatpoint Logo

Find Member Function in Vector in C++

Similar to an array in any other language, a vector in C++ is dynamic; hence its size is not constant. Why vectors? Because C++ arrays are static and cannot have their widths changed after being defined, this is not ideal when storing a data set whose size is unknown.

Example:

Output:

3
.........................................
Process executed in 1.22 seconds
Press any key to continue.

Explanation

This piece of code returns the element's index if it is present in the vector; else, it returns -1.

  • Line 1: The header file specified contains the locate function that we require, along with every C++ standard library.
  • Line 2: We define a function that has two inputs: a vector and a key to be searched.
  • Line 3: For our vector, we declare an iterator. It designates the vector's memory address. It will be utilized to traverse the vector. The link provided at the end of this post allows you to learn more about iterator.
  • Line 4: We call the std::find method, which will return an iterator with the location of key k in the vector.
  • Lines 5 through 8: Here, we do an if test to determine if the element is in the vector. If it is, we return its position; otherwise, we return -1, indicating that it is not.

The function call runs over n elements in the vector in order to locate the key we're looking for, hence the time complexity is linear O(n).

Simple comparisons are performed by iteratively walking over the vector with a constant O(1) space complexity.

Taking Help of std::find_if() with std::distance()

Finding an element in a vector using this method is advised if the search needs to fulfil a certain logic, such as finding an element's index in the vector using prime number logic.

When our predicate (compare struct) returns true for any element in the first to last range, std::find if() delivers an iterator to that element. The function will return end(last), point past last, if there is no such key.

The number of hops from first to last is returned by std::distance(). In our situation, it gives us the hops from the beginning of the vector up to the iterator's key, k, which is the positional index of the element we're looking for. The key index will depend on the number of hops.

Example:

Output:

3
.........................................
Process executed in 1.33 seconds
Press any key to continue.

Explanation

  • Line 2-8: We've declared a structure compare that evaluates the arguments' values against the int k.
  • The external link provided at the bottom of this post allows you to learn more about C++ structures.
  • Line 9: A searchResult function is declared, and its input parameters are a vector and a key.
  • Line 10: This time, we're using the STL library's find if method and passing it a comparison structure with the key k as a parameter, along with constant random access iterators that point to the vector's beginning (arr.cbegin()) and end (arr.cend()).

Next TopicSmart Pointer





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA