find_first_of()

I was reading about the find_first_of() function and I had a question about why it was written in a particular way.

http://www.cplusplus.com/reference/string/string/find_first_of/

The example program from the link above is written in this fashion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by asterisks.");
  std::size_t found = str.find_first_of("aeiou");
  while (found!=std::string::npos)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}


I think I have a basic understanding of how the above program functions. My question was why are they using std::string::npos instead of just -1?

and does STRING.find_first_of() always return -1 if there are no more characters in the string?

This produces the same output but I don't know if it might cause errors on more complicated programs if i don't use the std::string::npos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by asterisks.");
  std::size_t found = str.find_first_of("aeiou");
  while (found!=-1)
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}
Last edited on
I think I have a basic understanding of how the above program functions. My question was why are they using std::string::npos instead of just -1?

if I remember correctly std::string::npos doesn't equal -1. On my computer its 4294967295

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main()
{
	std::size_t foo = 0;
	std::cout << "npos = " << std::string::npos << std::endl;
	std::cout << "std::size_t = " << foo - 1 << std::endl;

	system("PAUSE");
	return(0);
}



std::string::npos is std::size_t -1 since size_t is an unsigned type -1 is not valid
so instead it gives the largest possible value size_t can represent
Last edited on
I wasn't sure. If it doesn't always work I'll continue to use the std::string::npos

I replaced it with -1 and the program worked fine on my compiler / system but if it doesn't work universally then I'll avoid simplifying it that much.
I think i figured out why it still works even with -1

4294967295 is the maximum number for the unsigned int

in binary it'd be 11111111111111111111111111111111

so -1 for an unsigned data type must be the same thing as going backwards from the maximum value for the data type?


that only seems to work some of the times.

Like if i wanted to check the first 13 letters for vowels and replace this would work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
  std::string str ("Please, replace the vowels in this sentence by asterisks.");
  std::size_t found = str.find_first_of("aeiou");
  while (found!=-4294967282) //4294967295 - 13
  {
    str[found]='*';
    found=str.find_first_of("aeiou",found+1);
  }

  std::cout << str << '\n';

  return 0;
}


but some of the other negatives numbers wouldn't work. I have a feeling this type of behavior could be useful at some point but I think it's beyond my abilities to make it useful at the current moment.

Thanks for help either way.
Last edited on
1 + 4294967295 = 0
1 + -1 = 0

yes -1 and 4294967295 has the same value, for unsigned int 32 bit.

std::size_t for 64 bit compiler is an unsigned int 64 bit. So -1 in unsigned int 64 bit is 2^64 - 1, way larger than 4294967295. Stick with std::string::npos. If you find that too long to type, just type str.npos (where str is a std::string)
Topic archived. No new replies allowed.