string class functions

I have used these before successfully, however I'm trying to understand them better: in the example of find http://www.cplusplus.com/reference/string/string/find/
The code checks for npos, before output. The API for npos suggests that npos is the end of string, so I don't really understand why this check is needed. if size_t != npos ...then

npos is a static member constant value with the greatest possible value for an element of type size_t.
This value, when used as the value for a count parameter n in string's member functions, roughly indicates "as many as possible".
When used in some pos parameters that allow for out-of-range values, npos indicates the end of the string.
As a return value it is usually used to indicate failure.
This constant is actually defined with a value of -1 (for any trait), which because size_t is an unsigned integral type, becomes the largest possible representable value for this type.
size_t
type
<cstddef>
Unsigned integral type
size_t corresponds to the integral data type returned by the language operator sizeof and is defined in the <cstddef> header file (among others) as an unsigned integral type.

It expresses a size or count in bytes.
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::find
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str ("There are two needles in this haystack with needles.");
  string str2 ("needle");
  size_t found;

  // different member versions of find in the same order as above:
  found=str.find(str2);
  if (found!=string::npos)
    cout << "first 'needle' found at: " << int(found) << endl;

  found=str.find("needles are small",found+1,6);
  if (found!=string::npos)
    cout << "second 'needle' found at: " << int(found) << endl;

  found=str.find("haystack");
  if (found!=string::npos)
    cout << "'haystack' also found at: " << int(found) << endl;

  found=str.find('.');
  if (found!=string::npos)
    cout << "Period found at: " << int(found) << endl;

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  cout << str << endl;

  return 0;
}
Last edited on
Yes, it is just a magic number.

In Tcl, for example, I can parse a string by saying I want everything from a specific index to the end:
1
2
set name "Johnny Five"
puts [string range $name 7 end]
Five

Or, I can specify (inclusive) indices for both:
1
2
set name "Johnny Five"
puts [string range $name 4 5]
ny

I suppose C++ could do something of similar sophistication, but simplicity is often the winner. Here, npos is just our magic number for end (since no string can be longer than that):
1
2
string name = "Johnny Five";
cout << name.substr( 7, string::npos );
Five


Likewise, if an item isn't found, why not indicate it with a magic number that cannot possibly be a valid result? In Tcl I would get a -1. In C++, I get the only number that cannot be a valid match: npos.

Etc.

So yes, you have it right. Hope this helps.
Cool thanks, I guess I just have to take it as a given.

My current assignment I have to parse a input file that has 2 commands and a puzzle. lines with whitespace are comments as are lines beginning with #
the two commands are 'size=x,y' and 'searchFor=word'
followed by a puzzle such as
akswordl
skokdjerm
srskdwor
dnskwww

The input file can contain errors as the puzzle can become before the size and search, which must be dealt with, and multiple puzzles throught the file.
So I thought using find would be the best way to parse the input file. as for the actual puzzle solving we are not allowed to use string or cstring (however even if we did I think it would complicate things). so just standard char[][], will be fun :( I got no idea how to do the puzzle.
Last edited on
The data structure is essentially a variation of the "matrix class" that beginners always get assigned, except now you are dealing with text instead of mathematics.

To find, simply scan through your rows and columns until you find the first letter of the word. Once found, look in the eight positions (maximum -- don't forget to account for the edges of the matrix) surrounding the (r,c) index for the second letter in your word. If found, check to see if all the letters in that direction follow the word. This is worth a little helper function/method.

To handle errors gracefully, you have a few options. Primarily, you can quit and complain that it is an error to list the puzzle before the size. The word to find can come anywhere.

Hope this helps.

This sounds like fun. Maybe I'll write my own.
Topic archived. No new replies allowed.