hey guys, could someone explain why you include string::size_type or string::npos? What do these do?

It's a type. A datatype. Like int or long or size_t or unsigned char.

You know how in C, size_t is an unsigned integer large enough to represent any array size possible? The C++ std::string class provides string::size_type as an integer datatype large enough to represent any possible string size.

The 'find' member function returns the location of the first occurrence of a substring or character in a string. This return value is an integer (whose type is string::size_type). If the substring or character is not found, it needs some value to return that signals this fact. A library designer could have said, "okay, let's return -1 when the substring's not found." But instead, they return some integer value hardcoded as string::npos. It's probably -1. Code that uses string::npos is more readable than code that uses -1. (Since you can't assume that string::npos == -1, you have to use string::npos anyway.)

commented: really nice explanation thank you +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.