string::find

I had a question about how string::find works

More specifically if a particular use of it could lead to problems in the future.

I'll post some code showing what I mean because it would probably be easier than describing it. It's a slightly modified version of this websites tutorial here. http://www.cplusplus.com/reference/string/string/find/

Here is one implementation. It loops through 4 times (because there are three "in"s in the quote"

My question is does the string::find function ever behave abnormally when a -1 is used to set the starting search position?

The code below functions fine although my counter is 1 higher than what it should be, but I can subtract 1 to get an accurate count.

I was thinking of doing something like this to create a dynamic array with counter number of elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <conio.h>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
  int pos = 0;                           // (quoting Alfred N. Whitehead)
  int counter = 0;

  while (str.find("in", pos) != std::string::npos){
        counter++;

        pos = str.find("in",pos+2);
        std::cout << "Position = " << pos << "\n";
        getch();  // wait

  }

  std::cout << "The number of \"in's\" in the quote are: " << counter - 1 << "\n\n\n";

  std::string str2 = str.substr (12,12);   // "generalities"

  pos = str.find("live");         // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}


If that's not safe I could probably do something like this.

If anybody has any thoughts or guidance on better ways to do this I am eager to learn :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// string::substr
#include <iostream>
#include <string>
#include <conio.h>

int main ()
{
  std::string str="We think in generalities, but we live in details.";
  int pos = 0;                           // (quoting Alfred N. Whitehead)
  int counter = 0;

  while (true){
                if (str.find("in",pos+2)!= std::string::npos){
                    counter++;
                    pos = str.find("in",pos+2);
                    std::cout << "Position = " << pos << "\n";
                    getch();  // wait
                }
                else {break;}

  }

  std::cout << "The number of \"in's\" in the quote are: " << counter << "\n\n\n";

  std::string str2 = str.substr (12,12);   // "generalities"

  pos = str.find("live");         // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}
Last edited on
Your code is probably ok. But I'd prefer to match the variable pos so it is of the same type as the return value from find(). Also, I'd prefer to call the find function only once in the loop where the occurrences are counted.

1
2
3
4
5
6
7
8
9
10
11
12
    std::string str="We think in generalities, but we live in details.";
    size_t pos = 0;                    
    int counter = 0;

    while ((pos = str.find("in", pos)) != std::string::npos)
    {
        counter++;
        std::cout << "Position = " << pos << "\n";
        pos++; 
    }

    std::cout << "The number of \"in's\" in the quote are: " << counter  << std::endl;


Topic archived. No new replies allowed.