Gice

Technology and General Blog

Distinctive created-in features exist in C++ to perform with the string data. The obtain_first_of() purpose is made use of to obtain the location of the 1st prevalence of the specified character.  This function returns the place of the to start with incidence of the string that will be given as the argument worth of this purpose. Distinctive takes advantage of of this function for browsing the string in C++ have been explained in this tutorial.

Pre-requisite

Ahead of examining the examples of this tutorial, you have to examine the g++ compiler is mounted or not in the technique. If you are making use of Visible Studio Code, then install the required extensions to compile the C++ resource code to generate the executable code. Here, the Visual Studio Code application has been employed to compile and execute the C++ code.

Characteristics of locate_to start with_of() function

The come across_initial_of() operate can return distinctive kinds of variables dependent on the value of the 1st argument. It will return the look for place of the string if the 1st argument benefit is a string. It will return the lookup place of the character string if the pointer to an array of figures is provided in the initial argument. It will return the buffer placement if the value of the 3rd argument is supplied. It will return character position if a character is given in the to start with argument and it exists in the major string. The setting up placement of the look for is established in the 2nd argument of this purpose. The various syntax of this functionality is specified underneath.

Syntax

string sizing_t find_initial_of (const string& str, dimension_t pos = ) const
c-string measurement_t locate_initially_of (const char* s, dimensions_t pos = ) const
buffer dimension_t locate_initially_of (const char* s, dimensions_t pos, size_t n) const
character size_t locate_1st_of (char c, measurement_t pos = ) const

Instance 1: Research and exchange the specific character of a string

The subsequent case in point reveals the way to research the place of the certain character in a string. Generate a C++ file with the following code to change the individual character with a different character. In the code, the obtain_initial_of() functionality has been applied to look for all positions of a specific character in a string and exchange the characters with a further character by employing the loop. The primary string and the changed string will be printed right after executing the code.

//Include for printing the output
#involve  
//Include things like for using sizing_t
#incorporate  

int most important ()

    //Initialize a string variable
    std::string strData (“Welcome to LinuxHint”)
    //Print the primary 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 >= )
   
        // Print the placement if the any character discovered
        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 list110, 5, 65, 31, 7
    std::vector list22, 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.

Leave a Reply

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