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?
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.
#include <string> const auto test = std::string("test"); if (test. contains('s')) { // found! }
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.
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.
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 string
s 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 char
s). 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 string
s: 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With