I don't think it would have necessarily been
harder to do it the way you had thought of (using
std::vectors) -- it would have just a bit more redundant, perhaps.
(But do try it out! If anything, it's good practice with
std::vectors :) .)
word.find(input)
returns the position of the first occurrence of the character
input in the string
word, or
std::string::npos if that character was not found.
Hence, by checking
if (pos != std::string::npos)
, we're essentially checking whether or not the user's input was found.
(Here, you can think of
npos as meaning "not found" or "the specified character is located at position 'no position' (npos)".)
You can write
std::string::npos instead of referring to a specific
std::string because
npos is, in a sense, "shared" by all
std::string objects (it's a
static
member constant).
It
is possible to write something like
1 2 3
|
std::string hi = "Hello";
if (hi.find('e') != hi.npos) // Access 'npos' through 'hi'
// ...
|
but I haven't seen anyone that actually does this.