How to use find_first_of() function in C++

Different built-in functions exist in C++ to work with the string data. The find_first_of() function is used to find the location of the first occurrence of the specified character.  This function returns the position of the first occurrence of the string that will be given as the argument value of this function. Different uses of this function for searching the string in C++ have been explained in this tutorial.

Pre-requisite

Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code.

Features of find_first_of() function

The find_first_of() function can return different types of variables based on the value of the first argument. It will return the search position of the string if the first argument value is a string. It will return the search position of the character string if the pointer to an array of characters is given in the first argument. It will return the buffer position if the value of the third argument is given. It will return character position if a character is given in the first argument and it exists in the main string. The starting position of the search is set in the second argument of this function. The different syntax of this function is given below.

Syntax

string size_t find_first_of (const string& str, size_t pos = 0) const;
c-string size_t find_first_of (const char* s, size_t pos = 0) const;
buffer size_t find_first_of (const char* s, size_t pos, size_t n) const;
character size_t find_first_of (char c, size_t pos = 0) const;

Example 1: Search and replace the particular character of a string

The following example shows the way to search the position of the particular character in a string. Create a C++ file with the following code to replace the particular character with another character. In the code, the find_first_of() function has been used to search all positions of a particular character in a string and replace the characters with another character by using the loop. The original string and the replaced string will be printed after executing the code.

//Include for printing the output
#include  
//Include for using size_t
#include  

int main ()
{
    //Initialize a string variable
    std::string strData ("Welcome to LinuxHint");
    //Print the original string
    std::cout << "The original string is: " + strData << '\n';
    //Find out all position of the character 'i'
    std::size_t searchList = strData.find_first_of("i");
    //Iterate the loop to replace all 'i' by '@'
    while (searchList!=std::string::npos)
    {
        strData[searchList] = '@';
        searchList = strData.find_first_of("i",searchList+1);
    }
    //Print the modified string
    std::cout << "The modified string is: " + strData << '\n';

    return 0;
}

Output:

The following output will appear after executing the above code.

Example 2: Search the first position of the searching characters 

Create a C++ file with the following code to search the position of a string of multiple characters and return the first position of the character that matches with the main string. A string data has been assigned in a string variable, and the first position is stored in an integer variable. The position value will be printed after executing the code.

//Include for printing the output
#include

int main()
{
    // Initialize the string variable
    std::string strData( "Basic C++ Programming");
    // Declare the interger variable to store the position
    int position;
    // Search the character 'C++'
    position = strData.find_first_of( "K++" );
    // Check the position value
    if (position >= 0)
    {
        // Print the position if the any character found
        std::cout << "The character '" << strData[position]
        << "' has found" << " at the position " << position << '\n';
    }

    return 0;
}

Output:

The following output will appear after executing the above code. Here, the character, ‘+’ of the string, ‘K++’ has found at the position, 7 of the main string, ‘Basic C++ Programming’.

Example 3: Search a particular character after the particular position

Create a C++ file with the following code to search a particular character after the particular position. A string data is stored in the string variable, and a particular string is searched after the position, 13. If any character of the searching string has been found in the main string, then a position value will be returned.

//Include for printing the output
#include

int main()
{
    //Initialize a string variable
    std::string strData = "eat to live, not live to eat";
    //Print the original string
    std::cout << "The original string is: " + strData << '\n';
    //Print the position where the last character found
    std::cout<<"The last matching character found at the position :"
    << strData.find_first_of("at",13) << '\n';
    return 0;
}

Output:

The following output will appear after executing the above code. Here, the character, ‘a’ of the string, ‘at’ has found at the position, 15 of the main string, ‘eat to live, not live to eat’.

Example 4: Search the position of the first matching number 

Create a C++ file with the following code to search each number of the first vector list in the second vector list and return the position of the matching number of the first vector list. If a matching argument is found, then the position value will be returned; otherwise, a message will be printed.

//Include for printing the output
#include
//Include for searching data in the vector
#include
//Include for using vector data
#include
int main()
{
    //Declare two vector list
    std::vector list1{10, 5, 65, 31, 7};
    std::vector list2{2, 77, 5, 38, 32, 55};
    //Search the data of list1 into the list2
    auto output = std::find_first_of(list1.begin(), list1.end(), list2.begin(), list2.end());
    // Read the position of the matching number
    int position = std::distance(list1.begin(), output);

    //Check any number of list1 match with any number of list2
    if (output < list1.end()) {
        std::cout << "The first matching number " << list1[position] << " found at      position "  << position << "\n";
    }
    else {
        std::cout << "No matching number found.\n";
    }
}

Output:

The following output will appear after executing the above code. The number value, 5 of the first array, exists in the second array and the position of this number is 1.

Conclusion

The find_first_of() function can be used to search a character or number for various programming purposes. This function can be used for solving the various searching problems. I hope the C++ programmer can be able to use this function properly after reading this tutorial.



from Linux Hint https://ift.tt/3eQAc6I

Post a Comment

0 Comments