7.7. The find function

The string class provides several other functions that you can invoke on strings. The find function is like the opposite of the [] operator. Instead of taking an index and extracting the character at that index, find takes a character and finds the index where that character appears.

Take a look at the active code below, which uses the find function to find the character 'a' in string fruit and string dessert.

The find function
 1#include <cstddef>
 2#include <iostream>
 3#include <string>
 4
 5using std::size_t;
 6
 7int main() {
 8    std::string fruit = "banana";
 9    std::size_t index = fruit.find('a');
10    std::cout << index << '\n';
11    std::string dessert = "pudding";
12    std::size_t another_index = dessert.find('a');
13    if (another_index == std::string::npos) {
14        std::cout << "not found\n";
15    } else {
16        std::cout << another_index << '\n';
17    }
18}

This example finds the index of the letter 'a' in the string. In this case, the letter appears three times, so it is not obvious what find should do. According to the documentation, it returns the index of the first appearance, so the result is 1. If the given letter does not appear in the string, find returns the special value std::string::npos, which is a number larger than any valid position in a string. Store the result in std::size_t and compare it with std::string::npos before using it as an index. The second search prints not found.

In addition, there is a version of find that takes another string as an argument and that finds the index where the substring appears in the string.

The active code below finds the starting index of "nan" in fruit.

The find function
 1#include <cstddef>
 2#include <iostream>
 3#include <string>
 4
 5using std::size_t;
 6
 7int main() {
 8    std::string fruit = "banana";
 9    std::size_t index = fruit.find("nan");
10    std::cout << index;
11}

This example returns the value 2.

You should remember from Section [overloading] that there can be more than one function with the same name, as long as they take a different number of parameters or different types. In this case, C++ knows which version of find to invoke by looking at the type of the argument we provide.

Q1

Click on the name of each variable that had been initialized with the value of 0.


    
    std::size_t  = fruit.find('e');
    std::size_t  = fruit.find("app");
    std::size_t  = fruit.find('a');
    std::size_t  = fruit.find('l');
}

Q2

Construct a block of code that correctly finds and prints where the first "B" is in the string. Declare city before index.

  1. cout << index << '\n';
  2. index = city.find('B');
  3. index = city.find('b'); #distractor
  4. index = city.find(B); #distractor
  5. int main() {
  6. std::size_t index;
  7. string city = "New Baltimore" #distractor
  8. string city = "New Baltimore";
  9. }

Q3

What is printed when the code is run?

string sentence = "Most seas are rough but this sea is so calm!";
string target = "sea";
size_t index = sentence.find(target);
cout << "Index to find sea is " << index << '\n';