This tutorial will discuss about unique ways to check if any element in array starts with string in C++.
Table Of Contents
Technique 1: Using std::find_if() function
Pass the start, and end iterators of array as first two arguments in STL Algorithm std::find_if()
. Also, pass a lambda function as the third argument in it.
The Lambda function accepts a string as argument, and check if the it starst with prefix string or not.
The std::find_if()
will apply the given lambda function on all elements of array one by one, till it returns true for an element. It will return an iterator to an element for which the lambda function returns true. If the lambda function returns false for all elements, then find_if()
will return iterator pointing to the end of array.
We can check the value of iterator returned by find_if()
to check it is valid or not. If it is valid, then it means array has at least one string that starts with the given prefix string.
Let’s see the complete example,
Frequently Asked:
- How to check if an Array is Sorted in C++
- Check if All elements of Array are equal in C++
- Check if all elements in array are false in C++
- Convert Char Array to an integer in C++
#include <iostream> #include <algorithm> #include <string> int main() { std::string arr[] = {"This", "is", "some", "random", "text", "today"}; std::string prefixStr = "ra"; // Check if any element in array starts with a string auto it = std::find_if( std::begin(arr), std::end(arr), [&prefixStr](const std::string& str) { return str.find(prefixStr) == 0; }); // Check if the returned iterator is valid if (it != std::end(arr)) { std::cout << " Yes, at least one element of Array starts with the prefix" << std::endl; } else { std::cout << " No, none of the Array element starts with the prefix" << std::endl; } return 0; }
Output :
Yes, at least one element of Array starts with the prefix
Technique 2: Using std::any_of() function
Suppose we have an array of strings, and a string value. Like this,
std::string arr[] = {"This", "is", "some", "random", "text", "today"}; std::string prefixStr = "ra";
Now we want to check if any element in array arr
starts with the a string prefixStr
.
We can use the STL Algorithm std::any_of() for it. For this we will pass following arguments,
- Iterator pointing to the start of array.
- Iterator pointing to the end of array.
- A Lambda function, which accepts a string as argument, and checks if this string starts with a specific string or not.
The std::any_of()
function will apply the given lambda function on all the elements of array. If the lambda function returns true for any element, then any_of()
function will also return true, which means that atleast one string in array starts with given prefix string.
Let’s see the complete example,
#include <iostream> #include <algorithm> #include <string> int main() { std::string arr[] = {"This", "is", "some", "random", "text", "today"}; std::string prefixStr = "ra"; // Check if any element in array starts with a string bool result = std::any_of( std::begin(arr), std::end(arr), [&prefixStr](const std::string& str) { return str.find(prefixStr) == 0; }); if (result) { std::cout << " Yes, at least one element of Array starts with the prefix" << std::endl; } else { std::cout << " No, none of the Array element starts with the prefix" << std::endl; } return 0; }
Output :
Yes, at least one element of Array starts with the prefix
Summary
Today we learned about several ways to check if any element in array starts with string in C++. Thanks.