Check if any element in array contains string in C++

This tutorial will discuss about a unique way to check if any element in array contains string in C++.

To check any string element in an array contains a sepcific string, we will use the std::any_of() function from STL Algorithms.

The std::any_of() function accepts three arguments,

  • Iterator pointing to the start of a sequence.
  • Iterator pointing to the end of a sequence.
  • A Callback or Lambda function which accepts a value of same type as the type of sequence, and returns a boolean value.

The std::any_of() function will apply the given Lambda function on all the elements of the sequence and it returns true if the callback function returns true for any element of sequence.

So, to check if any string in an array contains any specific string, we can use the std::any_of() function. For example, to check if any string in an array contains a string “day”, we will be using the following logic,

std::string arr[] = {"This", "is", "some", "random", "text", "today"};
std::string strValue = "day";

// Check if any string in Array contains a specific string
bool result = std::any_of(
                    std::begin(arr),
                    std::end(arr),
                    [&strValue](const std::string& elem) {
                                return elem.find(strValue) != std::string::npos;
                    });

We passed three arguments in the std::any_of() function,

  • Iterator pointing to the start of array i.e. iterator returned by std::begin(arr).
  • Iterator pointing to the end of array i.e. iterator returned by std::end(arr).
  • A Lambda function which accepts a string as an argument, and returns true if the given string contains a specific string.

The any_of() function will return true, if there is any string in the array that contains a specified 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 strValue = "day";

    // Check if any string in Array contains a specific string
    bool result = std::any_of(
                        std::begin(arr),
                        std::end(arr),
                        [&strValue](const std::string& elem) {
                                    return elem.find(strValue) != std::string::npos;
                        });

    if (result)
    {
        std::cout << " Yes, at least one element of Array contains a specific string" << std::endl;
    }
    else
    {
        std::cout << " No, none of the Array element contains a specific string" << std::endl;
    }

    return 0;
}

Output :

Yes, at least one element of Array contains a specific string

Summary

Today we learned about several ways to check if any element in array contains string in C++. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top