I'm writing a program to parse a user entered string. My textbook does not include the .find() function, but I found it in an old C++ book (Ivor Horton's Beginning C++, the Complete Language) is the .find() considered bad, or depricated? It seems to work really well, but I don't want to use things that are not up to standard, I want to avoid bad habits, as I've got enough I'm trying to break already.

Recommended Answers

All 4 Replies

The .find() function (in string class) is perfectly fine. I've never heard anybody say that there was anything wrong with it. It's a simple function to find a matching character or string within a string, what could go wrong with that?

In general, the only standard functions that one could say are deprecated or not recommended will be found within the legacy C libraries (any header starting with "c", like <cstdlib> or <cstring>) because some of them are not safe or have bad style.

Since C++11, one might argue that if you are doing a lot of string manipulations, such as finding particular tokens in a string, or some other general form of parsing, then it would be preferrable to use the <regex> (docs) library instead. However, regex is not widely available yet on compilers, and it's not the easiest library to work with. You only need for more serious parsing (an intermdiate between using simple string functions, and using a real parser generator library like Yacc, Bison or Boost.Spirit.

As a rule of thumb, every textbook won't cover every member function in the C++ standard library (not least because it's huge). For that sort of thing, look at a dedicated reference, such as:

http://en.cppreference.com/w/cpp/string/basic_string

There's find, towards the bottom. It's completely standard and there's nothing wrong with it. If it's what you need, use it. Make a note of that webpage; it's a pretty good C++ reference manual.

Basically, string::find() is akin to the C function char* strstr(const char* haystack, const char *needle);

There is some argument whether it should return a char* vs a const char* value. The Linux manpage shows it returning a char* - caveate user!

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.