Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does `std::string` have a `find` member function? [closed]

Why does std::string have a find member function while std::vector and friends don't have it?

Is there anything wrong with using std::find on the string?

like image 588
Frank Avatar asked Feb 12 '13 23:02

Frank


People also ask

How do I find a substring in a string C++?

Substring in C++ A function to obtain a substring in C++ is substr(). This function contains two parameters: pos and len. The pos parameter specifies the start position of the substring and len denotes the number of characters in a substring.

How do you check if a character is present in a string in C++?

#include <string> const auto test = std::string("test"); if (test. contains('s')) { // found! }

How does find work in C++?

string find in C++ String find is used to find the first occurrence of sub-string in the specified string being called upon. It returns the index of the first occurrence of the substring in the string from given starting position. The default value of starting position is 0.

How do you remove a character from a string in C++?

In C++ we can do this task very easily using erase() and remove() function. The remove function takes the starting and ending address of the string, and a character that will be removed.


1 Answers

That is mostly for historical reasons, but not only.

The String Library and the STL (which is the container/algorithm library developed by A. Stepanov that ended up being part of the C++ Standard Library) were developed independently, and they adopted different conventions.

However, since they eventually both converged into the C++ Standard Library, the C++ Standard does make an effort to unify these syntactic conventions and allows using strings with STL algorithms, which is why class string has member functions such as begin() and end() apart from other member functions such as substr().

Apart from backward compatibility, anyway, there is another reason why string provides member functions such as find(): unlike containers, which are meant to work with generic algorithms that access or manipulate their elements, strings are mostly thought of as values themselves rather than as collections of values (i.e. sequences of chars). Thus, it makes sense to encapsulate algorithms that manipulate string values into member functions of the string class.

In its design, the C++ Standard Library therefore supports both of these views of strings: as collections of values and as values themselves.

UPDATE:

The bit of your first sentence "while std::vector and friends don't have it" is not completely correct. At least, not if you extend std::vector's range of friends to std::set, std::multiset, std::map, std::multimap, std::unordered_set, and std::unordered_map (in other words, pretty much to all of the associative containers in the C++ Standard Library).

Certain data structures do indeed have a member-function version of some generic STL algorihtms on their interface: this is either to indicate the fact that those algorithms have a more efficient implementation than their generic counterparts for those particular data structures (e.g. find()), or that a specialized implementation is necessary because the generic algorithms cannot be applied at all to those data structures (e.g. std::remove(), which modifies the values in the container).

like image 115
Andy Prowl Avatar answered Nov 15 '22 18:11

Andy Prowl