Indian Technical Authorship Contest starts on 1st July 2023. Stay tuned.
In this article, we have explained Different Ways to find element in Vector in C++ STL which includes using std::find(), std::find_if, std::distance, std::count and Linear Search.
Table of contents:
- Introduction to Vector in C++ and STL
- How do we find an element using STL?
- Approach 1: Return index of the element using std::find()
- Use std::find_if() with std::distance()
- Use std::count()
- Linear search
Let us get started with Different Ways to find element in Vector in C++ STL.
Introduction to Vector in C++ and STL
A vector in C++ is just like an array in any other language but it is dynamic which means its size is not static. Why vectors? Well, arrays in C++ are static and once declared we cannot change their sizes which is not favourable when we need to store a data set whose size we are unsure of.
Unlike arrays we can determine the size of a vector at runtime and that is what makes vectors a perfect choice for these types of scenarios, they are dynamically allocated which means they can be resized.
The STL stands for Standard Template Library. Basically STL contains some generic functions that we shall need for this tutorial.
How do we find an element using STL?
There are three ways in which we can approach this problem:
Approach 1: By returning the index position of the element in the vector. Here we use std::find() and std::find_if()
Approach 2: By returning a boolean value, true if element is in the vector and false otherwise. Here we use std::count().
Approach 3: No STL here, we use linear search where by we go all through the vector elements making comparisons between the elements and key k.
Approach 1: Return index of the element using std::find()
std::find() searches for an element equal to the value that is passed as a parameter and returns an iterator pointing to that element in the vector.
In our case it will look like the following:
it = std::find(arr.begin(), arr.end(), k)
The parameter passed here is key k, the function call will return an iterator pointing to key k in the vector.
#include<bits/stdc++.h>
int searchResult(std::vector<int> arr, int k){
std::vector<int>::iterator it;
it = std::find(arr.begin(), arr.end(), k);
if(it != arr.end())
return (it - arr.begin());
else
return -1;
}
int main(){
std::vector<int> arr = {1, 2, 3, 4, 5, 6};
int k = 4;
std::cout << searchResult(arr, k) << std::endl;
return 0;
}
Code Explanation
This piece of code returns the index for the element we are trying to find and -1 if the element is not present in the vector.
Line 1: The header file declared includes every standard library in c++ and it contains the find function we need.
Line 2: We declare a function that takes a vector and key to be searched as parameters.
Line 3: We declare an iterator for our vector. It points at the memory address of the vector. We will use it for traversing the vector.
You can read up on iterators on the link provided at the end of this post.
Line 4: We make a function call to std::find that will return an iterator containing the position of key k in the vector.
Line 5 — 8: Here we do an if check, if the element is in the vector, we return its position otherwise we return -1 denoting that it is not in the vector.
The time complexity is linear O(n), this is because the function call goes through n items in the vector inorder to find the key we are searching for.
The space complexity is constant O(1), no extra space is used, just going through the vector and making comparisons.
2. Use std::find_if() with std::distance()
This is a recommended approach to finding an element in a vector if the search needs to satisfy a specific logic eg, finding an index of element in the vector that is a prime number using prime number logic.
std::find_if() returns an iterator to the first element in the first to last range for which our predicate(compare struct) returns true. If there is no such key, the function will return end(last), point past last.
std::distance() returns the number of hops from first to last. In our case it returns hops from begining of vector upto the iterator which points to the key k, this is the positional index of the element we are searching for. The number of hops will be the index of key k.
#include<bits/stdc++.h>
struct compare{
int k;
compare(int const &i): k(i) {}
bool operator()(int const &i) {
return (i == k);
}
};
int searchResult(std::vector<int> arr, int k){
auto itr = std::find_if(arr.cbegin(), arr.cend(), compare(k));
if(itr != arr.cend())
return std::distance(arr.cbegin(), itr);
return -1;
}
int main(){
std::vector<int> arr = {1, 2, 3, 4, 5, 6};
int k = 4;
std::cout << searchResult(arr, k) << std::endl;
return 0;
}
Code Explanation
The function searchResult() returns index of element in the vector or -1 denoting the absence of the element in the vector.
Line 2 — 8: We have declared a structure compare that compares the int k to values passed in its arguments.
You can read up on C++ structures at the external link provided at the end of this post.
Line 9: We declare a searchResult function that will take a vector and key as its parameters.
Line 10: Again we call the find_if function from STL libray but in this case we pass constant random access iterators that point to the begining (arr.cbegin()) and point past the end of the vector (arr.cend()) and the compare structure with key k as its parameter.
The auto keyword here specifies that the declared variable type will be automatically deducted from its initializer.
Line 10 — 13: We perform an if check to see if the iterator returned a value.
std::distance will perform as discussed above.
At this point it number of hops will be pointing to the index of key k in the vector.
The time complexity here is also linear O(n) because we got through the entire vector inorder to find the element.
The Space complexity is Constant O(1), no extra space is used.
Approach 2: Returning boolean value
3: Use std::count()
std::count() returns the number of occurences of an element in a vector.
We can apply it in situations where we need to check occurences of an element in the vector and return the number of times the element shows up in the vector.
#include<bits/stdc++.h>
bool searchResult(std::vector<int> arr, int k){
return std::count(arr.begin(), arr.end(), k);
}
int main(){
std::vector<int> arr = {1, 2, 3, 4, 5, 6};
int k = 4;
std::cout << searchResult(arr, k) << std::endl;
return 0;
}
Code Explanation
Line 2: The function searchResult() takes the vector arr and key k as its parameters.
It returns 1 denoting true and 0 denoting false.
Line 3: Count STL function counts occurences of key k, if there are any, it returns the number of element occurences.
We can do an if check, if the count is greater than 0 then tha element exists otherwise it doesnt.
The time complexity is linear O(n) as we go through the entire vector.
The space complexity is constant O(1) as no extra space is used.
Approach 3: Linear search
In this case we dont use any STL function but rather we traverse the vector using a for loop element by element and make comparisons between target and each element, if an element matches our target we return its index.
#include<bits/stdc++.h>
int searchResult(std::vector<int> arr, int k){
for(int i = 0; i < arr.size(); i++){
if(arr[i] == k){
return i;
}
}
return -1;
}
int main(){
std::vector<int> arr = {1, 4, 33, 44, 5, 6, 100, 4, 5, 7, 9, 0};
int k = 4;
std::cout << searchResult(arr, k) << std::endl;
return 0;
}
Code Explanation
The function takes a vector and key k as its parameters, if the element exists, it returns the index, otherwise it returns -1 denoting absence of the element.
Line 3: We use a for loop where we go through the vector making element and target comparisons.
Line 4: We do an if check, if the element at the index is equal to our target, we return the index, otherwise we exit the loop and return -1.
Question
What is the running time and space complexity of the above approach by linear search?
Can you explain why the time and space complexity is so?
References
- Iterators in C++
- Structures in C++
In this article we will different ways to find an element in vector and get its index.
Suppose we have a vector of int i.e.
Advertisements
std::vector<int> vecOfNums = { 12, 45, 54, 33, 2, 7, 8, 22, 43, 19 };
Now we want to find if number 22 exists in vector ? If yes then what’s its index or position in the vector ?
std::vector doesn’t provides any direct function to check if an element exists in vector or not. So let’s see how to do that using STL Algorithms.
Frequently Asked:
- vector::push_back() function in C++
- Convert a String to a Vector of Bytes in C++
- Importance of Constructors while using User Defined Objects with std::vector
- How to fill a vector with random numbers in C++
Finding an element in vector using STL Algorithm std::find()
Basically we need to iterate over all the elements of vector and check if given elements exists or not.
This can be done in a single line using std::find i.e.
// Check if element 22 exists in vector std::vector<int>::iterator it = std::find(vecOfNums.begin(), vecOfNums.end(), 22);
It accepts a range and an element to search in the given range. If element is found then it returns an iterator to the first element in the given range that’s equal to given element, else it returns an end of the list.
if (it != vecOfNums.end()) std::cout << "Element Found" << std::endl; else std::cout << "Element Not Found" << std::endl;
If element is found then we can get its index from the iterator i.e.
// Get index of element from iterator int index = std::distance(vecOfNums.begin(), it);
But in practical, we will not have vector of integers always. So, let’s create a generic function for this.
Generic function to find an element in vector of any type
Let’s create a generic function to search an element in any type of vector i.e.
/* Generic function to find an element in vector and also its position. It returns a pair of bool & int i.e. bool : Represents if element is present in vector or not. int : Represents the index of element in vector if its found else -1 */ template < typename T> std::pair<bool, int > findInVector(const std::vector<T> & vecOfElements, const T & element) { std::pair<bool, int > result; // Find given element in vector auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element); if (it != vecOfElements.end()) { result.second = distance(vecOfElements.begin(), it); result.first = true; } else { result.first = false; result.second = -1; } return result; }
This function tells if given element exists in vector and if yes then it also return its position in the vector.
Let’s use this function to find an element in vector i.e.
std::pair<bool, int> result = findInVector<int>(vecOfNums, 45); if (result.first) std::cout << "Element Found at index : " << result.second <<std::endl; else std::cout << "Element Not Found" << std::endl;
Finding an element by custom comparator using std::find_if()
Instead of directly searching by value in the vector , we can search by custom logic too.
Like, in a vector of int check if any multiple of 3 exists i.e.
// Check if any multiple of 3 exists in vector using lambda function as comparator std::vector<int>::iterator it2 = std::find_if(vecOfNums.begin(), vecOfNums.end(), [](const int & val){ if (val % 3 == 0) return true; return false; }); if (it != vecOfNums.end()) std::cout << "Multiple of 3 Found : " << *it2 << std::endl; else std::cout << "Multiple of 3 Not Found" << std::endl;
Finding an element in vector using C++11 Range Based for loop
We can also iterate over the vector using range based for loop and check if element exists or not.
bool found = false; // Iterate over all elements in Vector for (auto & elem : vecOfNums) { if (elem == 22) { found = true; break; } } if(found) std::cout << "Element Found" << std::endl; else std::cout << "Element Not Found" << std::endl;
Complete example is as follows,
#include <iostream> #include <vector> #include <algorithm> /* Generic function to find an element in vector and also its position. It returns a pair of bool & int i.e. bool : Represents if element is present in vector or not. int : Represents the index of element in vector if its found else -1 */ template < typename T> std::pair<bool, int > findInVector(const std::vector<T> & vecOfElements, const T & element) { std::pair<bool, int > result; // Find given element in vector auto it = std::find(vecOfElements.begin(), vecOfElements.end(), element); if (it != vecOfElements.end()) { result.second = distance(vecOfElements.begin(), it); result.first = true; } else { result.first = false; result.second = -1; } return result; } int main() { std::vector<int> vecOfNums = { 12, 45, 54, 33, 2, 7, 8, 22, 43, 19 }; /* Find an element in vector using std::find */ // Check if element 22 exists in vector std::vector<int>::iterator it = std::find(vecOfNums.begin(), vecOfNums.end(), 22); if (it != vecOfNums.end()) { std::cout << "Element Found" << std::endl; // Get index of element from iterator int index = std::distance(vecOfNums.begin(), it); std::cout <<"Index of element in vector : "<<index<<std::endl; } else { std::cout << "Element Not Found" << std::endl; } std::pair<bool, int> result = findInVector<int>(vecOfNums, 45); if (result.first) std::cout << "Element Found at index : " << result.second <<std::endl; else std::cout << "Element Not Found" << std::endl; /* * Finding an element by custom comparator */ // Check if any multiple of 3 exists in vector using lambda function as comparator std::vector<int>::iterator it2 = std::find_if(vecOfNums.begin(), vecOfNums.end(), [](const int & val){ if (val % 3 == 0) return true; return false; }); if (it != vecOfNums.end()) std::cout << "Multiple of 3 Found : " << *it2 << std::endl; else std::cout << "Multiple of 3 Not Found" << std::endl; /* Find an element in vector using c++11 range based for loop */ bool found = false; // Iterate over all elements in Vector for (auto & elem : vecOfNums) { if (elem == 22) { found = true; break; } } if(found) std::cout << "Element Found" << std::endl; else std::cout << "Element Not Found" << std::endl; return 0; }
Example
The function std::find
, defined in the <algorithm>
header, can be used to find an element in a std::vector
.
std::find
uses the operator==
to compare elements for equality. It returns an iterator to the first element in the range that compares equal to the value.
If the element in question is not found, std::find
returns std::vector::end
(or std::vector::cend
if the vector is const
).
C++11
static const int arr[] = {5, 4, 3, 2, 1};
std::vector<int> v (arr, arr + sizeof(arr) / sizeof(arr[0]) );
std::vector<int>::iterator it = std::find(v.begin(), v.end(), 4);
std::vector<int>::difference_type index = std::distance(v.begin(), it);
// `it` points to the second element of the vector, `index` is 1
std::vector<int>::iterator missing = std::find(v.begin(), v.end(), 10);
std::vector<int>::difference_type index_missing = std::distance(v.begin(), missing);
// `missing` is v.end(), `index_missing` is 5 (ie. size of the vector)
C++11
std::vector<int> v { 5, 4, 3, 2, 1 };
auto it = std::find(v.begin(), v.end(), 4);
auto index = std::distance(v.begin(), it);
// `it` points to the second element of the vector, `index` is 1
auto missing = std::find(v.begin(), v.end(), 10);
auto index_missing = std::distance(v.begin(), missing);
// `missing` is v.end(), `index_missing` is 5 (ie. size of the vector)
If you need to perform many searches in a large vector, then you may want to consider sorting the vector first, before using the binary_search
algorithm.
To find the first element in a vector that satisfies a condition, std::find_if
can be used. In addition to the two parameters given to std::find
, std::find_if
accepts a third argument which is a function object or function pointer to a predicate function. The predicate should accept an element from the container as an argument and return a value convertible to bool
, without modifying the container:
C++11
bool isEven(int val) {
return (val % 2 == 0);
}
struct moreThan {
moreThan(int limit) : _limit(limit) {}
bool operator()(int val) {
return val > _limit;
}
int _limit;
};
static const int arr[] = {1, 3, 7, 8};
std::vector<int> v (arr, arr + sizeof(arr) / sizeof(arr[0]) );
std::vector<int>::iterator it = std::find_if(v.begin(), v.end(), isEven);
// `it` points to 8, the first even element
std::vector<int>::iterator missing = std::find_if(v.begin(), v.end(), moreThan(10));
// `missing` is v.end(), as no element is greater than 10
C++11
// find the first value that is even
std::vector<int> v = {1, 3, 7, 8};
auto it = std::find_if(v.begin(), v.end(), [](int val){return val % 2 == 0;});
// `it` points to 8, the first even element
auto missing = std::find_if(v.begin(), v.end(), [](int val){return val > 10;});
// `missing` is v.end(), as no element is greater than 10
Something like this, I think. find_if_counted.hpp
:
#ifndef FIND_IF_COUNTED_HPP
#define FIND_IF_COUNTED_HPP
#include <algorithm>
namespace find_if_counted_impl
{
template <typename Func>
struct func_counter
{
explicit func_counter(Func& func, unsigned &count) :
_func(func),
_count(count)
{
}
template <typename T>
bool operator()(const T& t)
{
++_count;
return _func(t);
}
private:
Func& _func;
unsigned& _count;
};
}
// generic find_if_counted,
// returns the index of the found element, otherwise returns find_if_not_found
const size_t find_if_not_found = static_cast<size_t>(-1);
template <typename InputIterator, typename Func>
size_t find_if_counted(InputIterator start, InputIterator finish, Func func)
{
unsigned count = 0;
find_if_counted_impl::func_counter<Func> f(func, count);
InputIterator result = find_if(start, finish, f);
if (result == finish)
{
return find_if_not_found;
}
else
{
return count - 1;
}
}
#endif
Example:
#include "find_if_counted.hpp"
#include <cstdlib>
#include <iostream>
#include <vector>
typedef std::vector<int> container;
int rand_number(void)
{
return rand() % 20;
}
bool is_even(int i)
{
return i % 2 == 0;
}
int main(void)
{
container vec1(10);
container vec2(10);
std::generate(vec1.begin(), vec1.end(), rand_number);
std::generate(vec2.begin(), vec2.end(), rand_number);
unsigned index = find_if_counted(vec1.begin(), vec1.end(), is_even);
if (index == find_if_not_found)
{
std::cout << "vec1 has no even numbers." << std::endl;
}
else
{
std::cout << "vec1 had an even number at index: " << index <<
" vec2's corresponding number is: " << vec2[index] << std::endl;
}
}
Though I feel like I’m doing something silly… :X Any corrections are welcome, of course.
This tutorial will go through how to find an element in a C++ vector and how to count the number of occurrences of an element with the help of code examples.
Table of contents
- A Brief Introduction on Vectors in C++
- Find Element in C++ Vector Using std::find()
- Return Index of the Element Using std::find_if()
- Count the Number of Occurrences of an Element Using std::count()
- Return Boolean Using std::any_of()
- Summary
A Brief Introduction on Vectors in C++
A vector is a dynamic container for storing data. Unlike arrays, the size of a vector can grow and shrink dynamically during the execution of a program. Vectors are part of the C++ Standard Template Library. To use vectors, we need to include the vector header file in our program like so
#include <vector>
Let’s look at how to find elements in a C++ vector.
Find Element in C++ Vector Using std::find()
The function std::find() searches for an element equal to the value passed as a parameter and returns an iterator pointing to that element in the vector. The syntax of find() is as follows:
find(first, last, value)
Where first and last are the input iterators to the initial and final positions in a sequence. The function searches for value in all elements between first and last including the element pointed by first but not the element pointed by last.
Let’s look at an example where we want to find an integer in an array of integers.
#include <iostream> #include <vector> int find_element(std::vector<int> arr, int k){ std::vector<int>::iterator it; it = std::find(arr.begin(), arr.end(), k); if(it != arr.end()) return (it - arr.begin()); else return -1; }
In the above code, we will define a function that takes a vector and key to search for as parameters. We declare an iterator for our vector which points to the memory address of the vector. We use this iterator to traverse the vector. Then, we call std::find
to return the position of key k in the vector. Using an if statement we return the position of the element if it exists in the vector otherwise we return -1 meaning it is not in the vector.
Let’s define a vector of int in the main()
function and call the find_element
to find the number 6.
int main() { std::vector<int> arr = {2, 4, 6, 8, 10}; int k = 6; std::cout << find_element(arr, k) << std::endl; }
Let’s run the code to get the result:
2
The number 6 is at position 2 in the vector.
You can test your C++ code using our free C++ compiler and code executor.
Return Index of the Element Using std::find_if()
We can find the index of an element in a vector using the std::find_if()
function. This function finds the first element in a range for which the predicate function returns true. Let’s look at an example where use find_if()
the first element in a vector of integers that is a multiple of 5.
#include <iostream> #include <vector> // Returns true if x is a multiple of 5 bool multipleOf5(int x) { return x % 5 == 0; }; int main(){ std::vector<int> arr = {2, 4, 6, 8, 10, 12, 20}; std::vector<int>::iterator mult5 = std::find_if(arr.begin(), arr.end(), multipleOf5); std::cout << "First item that is a multiple of 5: " << *mult5 << std::endl; return 0; }
Count the Number of Occurrences of an Element Using std::count()
The function std::count() returns the number of occurrences of an element in a vector. The syntax of std::count is as follows:
count(first, last, value)
Where first and last are the input iterators to the initial and final positions in a sequence. The function searches for value in all elements between first and last, including the element pointed by first but not the element pointed by last.
Let’s look at an example:
#include <iostream> #include <vector> bool count_elements(std::vector<int> arr, int k){ return std::count(arr.begin(), arr.end(), k); }
In the above code, the function count_elements()
takes a vector of int and a key as its parameters.
#include <iostream> #include <vector> bool count_elements(std::vector<int> arr, int k){ return std::count(arr.begin(), arr.end(), k); } int main(){ std::vector<int> arr = {2, 4, 6, 8, 10, 12}; int k = 6; std::cout << count_elements(arr, k) << std::endl; return 0; }
1
Return Boolean Using std::any_of()
The std::any_of
function works similarly to the std::find_if()
function. The any_of()
function returns a boolean value True if the predicate returns True
for any of the elements and False
otherwise.
Let’s look at an example where use any_of()
to check if any of the elements in a vector of integers is a multiple of 5.
#include <iostream> #include <vector> // Returns true if x is a multiple of 5 bool multipleOf5(int x) { return x % 5 == 0; }; int main(){ std::vector<int> arr = {2, 4, 6, 8, 10, 12, 20}; if (std::any_of(v.begin(), v.end(), multipleOf5)) { std::cout << "Element found"; } else { std::cout << "Element not found"; } return 0; }
Element found
Summary
Congratulations on reading to the end of this tutorial! We have gone through several ways to check if an element exists in a vector and how to count the number of occurrences of an element in a vector.
For further reading on C++, go to the articles:
- How to Convert Char Array to String in C++
- How to Reverse a String in C++
- How to Sort a Vector in C++
- How to Iterate Through a Vector in C++
Have fun and happy researching!