Among the facilitiesC++ programmers have developed over and over againare those manipulating chunks of text, commonly calledstrings. TheCprogramming language offers rudimentary string support.
To process textC++ offers astd::string type. InC++the traditionalC library functions manipulating NTB strings aredeprecated in favor of usingstring objects. Many problems inCprograms are caused by buffer overruns, boundary errors and allocationproblems that can be traced back to improperly using these traditionalCstring library functions. Many of these problems can be prevented usingC++ string objects.
Actually,string objects areclass type variables, and in that sensethey are comparable to stream objects likecin andcout. In thissection the use ofstring type objects is covered. The focus is on theirdefinition and their use. When usingstring objects themember functionsyntax is commonly used:
stringVariable.operation(argumentList)
For example, ifstring1 andstring2 are variables of typestd::string, then
string1.compare(string2)
can be used to compare both strings.
In addition to the common member functions thestring class also offers awide variety ofoperators, like the assignment (=) and the comparisonoperator (==). Operators often result in code that is easy to understandand their use is generally preferred over the use of member functions offeringcomparable functionality. E.g., rather than writing
if (string1.compare(string2) == 0)
the following is generally preferred:
if (string1 == string2)
To define and usestring-type objects, sources must include the headerfile<string>. To merelydeclare the string typethe headeriosfwd can be included.
In addition tostd::string, the header filestring defines thefollowing string types:
std::wstring, a string type consisting ofwchar_t characters;std::u16string, a string type consisting ofchar16_t characters;std::u32string, a string type consisting ofchar32_t characters.string::npos is returned. This value is a symbolicvalue of typestring::size_type, which is (for all practicalpurposes) an (unsigned)int.Allstring member functions acceptingstring objects as arguments alsoaccept NTBS arguments. The same usually holds true for operators acceptingstring objects.
Somestring-members useiterators. Iterators are formally introducedin section18.2. Member functions using iterators are listed inthe next section (5.2), but the iterator concept itselfis not further covered by this chapter.
Strings support a large variety of members and operators. A short overviewlisting their capabilities is provided in this section, with subsequentsections offering a detailed discussion. The bottom line:C++ strings areextremely versatile and there is hardly a reason for falling back on theClibrary to process text.C++ strings handle all the required memorymanagement and thus memory related problems, which are the #1 source ofproblems inC programs, can be prevented whenC++ strings areused. Strings do come at a price, though. The class's extensive capabilitieshave also turned it into a beast. It's hard to learn and master all itsfeatures and in the end you'll find that not all that you expected is actuallythere. For example,std::string doesn't offer case-insensitivecomparisons. But in the end it isn't even as simple as that. Itis there,but it is somewhat hidden and at this point in theC++ Annotations it's tooearly to study into that hidden corner yet. Instead, realize thatC'sstandard librarydoes offer useful functions that can be used as long aswe're aware of their limitations and are able to avoid their traps. So fornow, to perform a traditionalcase-insensitive comparison of the contentof twostd::string objectsstr1 andstr2 the following will do:
strcasecmp(str1.c_str(), str2.c_str());
Strings support the following functionality:
initialization:assignment:assign) but a plain assignment operator (i.e.,=)may also be used. Furthermore, assignmentto a character buffer isalso supported.conversions:std::strings are accepted as well.breakdown:[]) allowing us to either access ormodify information in the middle of a string.comparisons:==, !=, <, <=, > and>=. Thereare also member functions available offering a more fine-grained comparison.modification:swapping:searching:housekeeping:stream I/O:object is always astring-object;argument is astring const & or achar const * unlessindicated otherwise. The content of anargument never is modified by theoperation processing theargument;opos refers to an offset into anobject string;apos refers to an offset into anargument;on represents a number of characters in anobject (startingatopos);an represents a number of characters in anargument (startingatapos).Bothopos andapos must refer to existing offsets, or an exception(cf. chapter10) is generated. In contrast,an andon mayexceed the number of available characters, in which case only the availablecharacters are considered.
Many members declare default values foron, an andapos. Some membersdeclare default values foropos. Default offset values are 0, the defaultvalues ofon andan isstring::npos, which can be interpreted as`the required number of characters to reach the end of the string'.
With members starting their operations at the end of the string object'scontent proceeding backwards, the default value ofopos is the index ofthe object'slast character, withon by default equal toopos + 1,representing the length of the substringending atopos.
In the overview of member functions presented below it may be assumed that allthese parameters accept default values unless indicated otherwise. Of course,the default argument values cannot be used if a function requires additionalarguments beyond the ones otherwise accepting default values.
Some members have overloaded versions expecting an initial argument of typechar const *. But even if that is not the case the first argument canalways be of typechar const * where a parameter ofstd::string isdefined.
Several member functions acceptiterators. Section18.2 coversthe technical aspects ofiterators, but these may be ignored at this pointwithout loss of continuity. Likeapos andopos, iterators must referto existing positions and/or to an existing range of characters within thestring object's content.
Allstring-member functions computing indices return thepredefined constantstring::npos on failure.
Thesliteral suffix to indicate that astd::string constant isintended when a string literal (like"hello world") is used. It can beused after declaringusing namespace std or, more specific, afterdeclaringusing namespace std::literals::string_literals.
When string literals are used when explicitly defining or usingstd::string objects thes-suffix is hardly ever required, but it maycome in handy when using theauto keyword. E.g.,auto str = "helloworld"s definesstd::string str, whereas it would have been acharconst * if the literal suffix had been omitted.
string constructors areavailable:string object:object to an empty string. When defining astring this way no argument list may be specified;string object(string::size_type count, char ch):object withcount charactersch.Caveat: to initialize a string object using this constructor do not use the curly braces variant, but use the constructor as shown, to avoid selecting the initializer-list constructor (see below);string object(string const &argument):object withargument;string object(std::string const &argument, string::size_type apos, string::size_type an):object withargument's content starting at index positionapos, using at mostan ofargument's characters;string object(InputIterator begin, InputIterator end):object with the characters in the range of characters defined by the twoInputIterators.string object(std::initializer_list<char> chars):object with the characters specified in the initializer list. The string may also directly be initialized, using the curly braced initialization. Here is an example showing both forms:string str1({'h', 'e', 'l', 'l', 'o'});string str2{ 'h', 'e', 'l', 'l', 'o' };Iterators play an important role in the context ofgeneric algorithms(cf. chapter19). The classstd::string defines the followingiterator types:
string::iterator andstring::const_iterator:these iterators areforward iterators. Theconst_iteratoris returned bystring constobjects, the plainiteratoris returned by non-const string objects. Characters referred tobyiteratorsmay be modified;
string::reverse_iterator andstring::const_reverse_iterator:these iterators are alsoforward iterators but whenincrementing the iterator theprevious character in the string objectis reached. Other than that they are comparable to, respectively,string::iteratorandstring::const_iterator.
The following operators are available forstring objects (in the examples`object' and `argument' refer to existingstd::string objects).
a character,C orC++ string may be assigned to astringobject. The assignment operator returns its left-hand side operand. Example:object = argument;object = "C string";object = 'x';object = 120; // same as object = 'x'
the arithmetic additive assignment operator and the additionoperator add text to astringobject. The compound assignment operatorreturns its left-hand side operand, the addition operator returns its resultin a temporary string object. When using the addition operator either theleft-hand side operand or the right-hand side operand must be astd::stringobject. The other operand may be a char, aC string or aC++ string. Example:object += argument;object += "hello";object += 'x'; // integral expressions are OKargument + otherArgument; // two std::string objectsargument + "hello"; // using + at least one"hello" + argument; // std::string is requiredargument + 'a'; // integral expressions are OK'a' + argument;
The index operator may be used to retrieveobject'sindividual characters, or to assign new values to individual characters of anon-const string object. There is no range-checking (use theat()memberfunction for that). This operator returns achar &orchar const &.Example:object[3] = argument[5];
the logical comparison operators may be applied to two stringobjects or to a string object and aC string to compare theircontent. These operators return aboolvalue.The==, !=, >, >=, <,and<=operators are available. The orderingoperators perform a lexicographical comparison of their contentusing the ASCII character collating sequence. Example:object == object; // trueobject != (object + 'x'); // trueobject <= (object + 'x'); // true
the insertion-operator (cf. section3.1.4) may be used toinsert astringobject into anostream, the extraction-operator may beused to extract a string object from anistream. The extraction operatorby default first ignores all whitespace characters and then extracts allconsecutively non-blank characters from anistream. Instead of a string acharacter array may be extracted as well, but the advantage of using a stringobject should be clear: the destination string object is automatically resizedto the required number of characters. Example:cin >> object;cout << object;
std::string class offers many member function as well as additionalnon-member functions that should be considered part of the string class.All these functions are listed below in alphabetic order.The symbolic valuestring::npos is defined by the string class. Itrepresents `index-not-found' when returned by member functions returningstring offset positions. Example: when calling `object.find('x')' (seebelow) on a string object not containing the character'x',nposis returned, as the requested position does not exist.
The final 0-byte used inC strings to indicate the end of an NTBS isnot considered part of aC++ string, and so the member function willreturnnpos, rather thanlength() when looking for 0 in a stringobject containing the characters of aC string.
Here are the standard functions that operate on objects of the classstring. When a parameter ofsize_t is mentioned it may be interpreted as aparameter of typestring::size_type, but without defining a defaultargument value. The typesize_type should be read asstring::size_type. Withsize_type the default argument valuesmentioned in section5.2 apply. All quoted functions aremember functions of the classstd::string, except where indicatedotherwise.
char &at(size_t opos):string const objects achar const & is returned. The member function performs range-checking, raising an exception (that by default aborts the program) if an invalid index is passed.string &append(InputIterator begin, InputIterator end):begin andend are appended to the current string object.string &append(string const &argument, size_type apos, size_type an):argument (or a substring) is appended to the current string object.string &append(char const *argument, size_type an):an characters ofargument are appended to the string object.string &append(size_type n, char ch):n charactersch are appended to the current string object.string &assign(string const &argument, size_type apos, size_type an):argument (or a substring) is assigned to the string object. Ifargument is of typechar const * and one additional argument is provided the second argument is interpreted as a value initializingan, using 0 to initializeapos.string &assign(size_type n, char ch):n charactersch are assigned to the current string object.char &back():char stored inside the string object. The result is undefined for empty strings.string::iterator begin():const string objects aconst_iterator is returned.size_type capacity() const:string::const_iterator cbegin():const_iterator referring to the first character of the current string object is returned.string::const_iterator cend():const_iterator referring to the end of the current string object is returned.void clear():int compare(string const &argument) const:argument is compared using a lexicographical comparison using the ASCII character collating sequence. zero is returned if the two strings have identical content, a negative value is returned if the text in the current object should be orderedbefore the text inargument; a positive value is returned if the text in the current object should be orderedbeyond the text inargument.int compare(size_t opos, size_t on, string const &argument) const:argument. At moston characters starting at offsetopos are compared to the text inargument.int compare(size_t opos, size_t on, string const &argument, size_type apos, size_type an):argument. At moston characters of the current string object, starting at offsetopos, are compared to at mostan characters ofargument, starting at offsetapos. In this caseargumentmust be a string object.int compare(size_t opos, size_t on, char const *argument, size_t an):argument. At moston characters of the current string object starting at offsetopos are compared to at mostan characters ofargument.Argument must have at leastan characters. The characters may have arbitrary values: 0-valued characters have no special meanings.bool contains(argument) const:true if the object containsargument's characters as a substring. The argument can be astring, astring_view (see section5.3), achar or an NTBS.size_t copy(char *argument, size_t on, size_type opos) const:argument. The actual number of characters copied is returned. The second argument, specifying the number of characters to copy, from the current string object is required. No 0-valued character is appended to the copied string but can be appended to the copied text using an idiom like the following:argument[object.copy(argument, string::npos)] = 0;Of course, the programmer should make sure that
argument's size is large enough to accommodate the additional 0-byte.string::const_reverse_iterator crbegin():const_reverse_iterator referring to the last character of the current string object is returned.string::const_reverse_iterator crend():const_reverse_iterator referring to the begin of the current string object is returned.char const *c_str() const:char const *data() const:c_str does), it can be used to retrieve any kind of information stored inside the current string object including, e.g., series of 0-bytes:string s(2, 0); cout << static_cast<int>(s.data()[1]) << '\n';
bool empty() const:true is returned if the current string object contains no data.string::iterator end():const string objects aconst_iterator is returned.bool ends_with(argument) const:true if the object's characters end withargument. The argument can be astring, astring_view, achar or an NTBS.string &erase(size_type opos, size_type on):string::iterator erase(string::iterator begin, string::iterator end):end is optional. If omitted the value returned by the current object'send member is used. The characters defined by thebegin andend iterators are erased. The iteratorbegin is returned, which is then referring to the position immediately following the last erased character.size_t find(string const &argument, size_type opos) const:argument is found.size_t find(char const *argument, size_type opos, size_type an) const:argument is found. When all three arguments are specified the first argumentmust be achar const *.size_t find(char ch, size_type opos) const:ch is found.size_t find_first_of(string const &argument, size_type opos) const:argument.size_type find_first_of(char const *argument, size_type opos, size_type an) const:argument. Ifopos is provided it refers to the first index in the current string object where the search forargument should start. If omitted, the string object is completely scanned. Ifan is provided it indicates the number of characters of thechar const * argument that should be used in the search. It defines a substring starting at the beginning ofargument. If omitted, all ofargument's characters are used.size_type find_first_of(char ch, size_type opos):ch.size_t find_first_not_of(string const &argument, size_type opos) const:argument.size_type find_first_not_of(char const *argument, size_type opos, size_type an) const:argument. Theopos andan parameters are handled as withfind_first_ofsize_t find_first_not_of(char ch, size_type opos) const:ch.size_t find_last_of(string const &argument, size_type opos) const:argument.size_type find_last_of(char const *argument, size_type opos, size_type an) const:argument. Ifopos is provided it refers to the last index in the current string object where the search forargument should start (searching backward towards the beginning of the current object). If omitted, the string object is scanned completely. Ifan is provided it indicates the number of characters of thechar const * argument that should be used in the search. It defines a substring starting at the beginning ofargument. If omitted, all ofargument's characters are used.size_type find_last_of(char ch, size_type opos):ch.size_t find_last_not_of(string const &argument, size_type opos) const:argument.size_type find_last_not_of(char const *argument, size_type opos, size_type an) const:argument. Theopos andan parameters are handled as withfind_last_of.size_t find_last_not_of(char ch, size_type opos) const:ch.char &front():char stored inside the string object. The result is undefined for empty strings.allocator_type get_allocator():std::stringistream &std::getline(istream &istr, string &object, char delimiter = '\n'):string.istr. All characters untildelimiter (or the end of the stream, whichever comes first) are read fromistr and are stored inobject. If the delimiter is encountered it is removed from the stream, but is not stored inobject.istr.eof returnstrue (see section6.3.1). Since streams may be interpreted asbool values (cf. section6.3.1) a commonly encountered idiom to read all lines from a stream successively into a string objectline looks like this:while (getline(istr, line)) process(line);The content of the last line, whether or not it was terminated by a delimiter, is eventually also assigned to
object.string &insert(size_t opos, string const &argument, size_type apos, size_type an):argument is inserted into the current string object at the current string object's index positionopos. Arguments forapos andan must either both be provided or they must both be omitted.string &insert(size_t opos, char const *argument, size_type an):argument (of typechar const *) is inserted at indexopos into the current string object.string &insert(size_t opos, size_t count, char ch):Count charactersch are inserted at indexopos into the current string object.string::iterator insert(string::iterator begin, char ch):ch is inserted at the current object's position referred to bybegin.Begin is returned.string::iterator insert(string::iterator begin, size_t count, char ch):Count charactersch are inserted at the current object's position referred to bybegin.Begin is returned.string::iterator insert(string::iterator begin, InputIterator abegin, InputIterator aend):InputIterators abegin andaend are inserted at the current object's position referred to bybegin.Begin is returned.size_t length() const:size_t max_size() const:void pop_back():void push_back(char ch):ch is appended to the string object.string::reverse_iterator rbegin():const string objects aconst_reverse_iterator is returned.string::reverse_iterator rend():const string objects aconst_reverse_iterator is returned.string &replace(size_t opos, size_t on, string const &argument, size_type apos, size_type an):object are replaced by the (subset of) characters ofargument. Ifon is specified as 0argument is inserted intoobject at offsetopos.string &replace(size_t opos, size_t on, char const *argument, size_type an):object are replaced by the firstan characters ofchar const * argument.string &replace(size_t opos, size_t on, size_type count, char ch):on characters of the current string object, starting at index positionopos, are replaced bycount charactersch.string &replace(string::iterator begin, string::iterator end, string const &argument):begin andend are replaced byargument. Ifargument is achar const *, an additional argumentan may be used, specifying the number of characters ofargument that are used in the replacement.string &replace(string::iterator begin, string::iterator end, size_type count, char ch):begin andend are replaced bycount characters having valuesch.string &replace(string::iterator begin, string::iterator end, InputIterator abegin, InputIterator aend):begin andend are replaced by the characters in the range defined by theInputIterators abegin andaend.void reserve(size_t request):request. After calling this member,capacity's return value will be at leastrequest. A request for a smaller size than the value returned bycapacity is ignored. Astd::length_error exception is thrown ifrequest exceeds the value returned bymax_size (std::length_error is defined in thestdexcept header). Callingreserve() has the effect of redefining a string's capacity: when enlarging the capacity extra memory is allocated, but not immediately available to the program. This is illustrated by the exception thrown by the string'sat() member when trying to access an element exceeding the string'ssize but not the string'scapacity.void resize(size_t size, char ch = 0):size characters. If the string object is resized to a size larger than its current size the additional characters will be initialized toch. If it is reduced in size the characters having the highest indices are chopped off.size_t rfind(string const &argument, size_type opos) const:argument is found is returned. Searching proceeds from the current object's offsetopos back to its beginning.size_t rfind(char const *argument, size_type opos, size_type an) const:argument is found is returned. Searching proceeds from the current object's offsetopos back to its beginning. The parameteran specifies the length of the substring ofargument to look for, starting atargument's beginning.size_t rfind(char ch, size_type opos)const:ch is found is returned. Searching proceeds from the current object's offsetopos back to its beginning.void shrink_to_fit(): string{ stringObject }.swap(stringObject) idiom can be used.size_t size() const:length().bool starts_with(argument) const:true if the object's character range starts withargument. The argument can be astring, astring_view, achar or an NTBS.string substr(size_type opos, size_type on) const:on characters starting at indexopos is returned.void swap(string &argument):argument. For this memberargument must be a string object and cannot be achar const *.std::string objects. These functions are listed below in alphabeticorder. They are not member functions, but class-less (free) functions declaredin thestd namespace. The<string> header file must be includedbefore they can be used.float stof(std::string const &str, size_t *pos = 0):str are ignored. Then the following sequences of characters are converted to afloat value, which is returned:inf orinfinity (case insensitive words)nan ornan(alphanumeric character sequence) (nan is a case insensitive word), resulting in aNaN floating point valuepos != 0 the index of the first character instr which was not converted is returned in*pos. Astd::invalid_argument exception is thrown if the characters instr could not be converted to afloat, astd::out_of_range exception is thrown if the converted value would have exceeded the range offloat values.double stod(std::string const &str, size_t *pos = 0):stof is performed, but now to a value of typedouble.double stold(std::string const &str, size_t *pos = 0):stof is performed, but now to a value of typelong double.int stoi(std::string const &str, size_t *pos = 0, int base = 10):str are ignored. Then all characters representing numeric constants of the number system whosebase is specified are converted to anint value, which is returned. An optional + or - character may prefix the numeric characters. Values starting with 0 are automatically interpreted as octal values, values starting with 0x or 0X as hexadecimal characters. The valuebase must be between 2 and 36. Ifpos != 0 the index of the first character instr which was not converted is returned in*pos. Astd::invalid_argument exception is thrown if the characters instr could not be converted to anint, astd::out_of_range exception is thrown if the converted value would have exceeded the range ofint values.Here is an example of its use:
int value = stoi(" -123"s); // assigns value -123value = stoi(" 123"s, 0, 5); // assigns value 38long stol(std::string const &str, size_t *pos = 0, int base = 10):stoi is performed, but now to a value of typelong.long long stoll(std::string const &str, size_t *pos = 0, int base = 10):stoi is performed, but now to a value of typelong long.unsigned long stoul(std::string const &str, size_t *pos = 0, int base = 10):stoi is performed, but now to a value of typeunsigned long.unsigned long long stoull(std::string const &str, size_t *pos = 0, int base = 10):stoul is performed, but now to a value of typeunsigned long long.std::string to_string(Type value):int, long, long long, unsigned, unsigned long, unsigned long long, float, double, orlong double. The value of the argument is converted to a textual representation, which is returned as astd::string value.std::wstring to_wstring(Type value):to_string is performed, returning astd::wstring.std::string the classstd::string_view can beused as a wrapper-class ofchar arrays. The classstring_view canbe considered a light-weightstring class. Before usingstd::string_view objects the<string_view> header file must have beenincluded.In addition to the standard constructors (default, copy, move) it offers thefollowing constructors:
constexpr string_view(char const *src, size_t nChars), constructs astring_view object from the firstnChars characters ofsrc. Thecharacters in the range[src, src + nChars) may be 0-valuedcharacters;constexpr string_view(char const *src), constructs astring_viewobject from the NTBS starting atsrc. The argument passed to thisconstructor may not be a null pointer;constexpr string_view(Iterator begin, Iterator end), constructs astring_view object from the characters in the iterator-range[begin, end).string_view object does not contain its own copy of the initializeddata. Instead, it refers to the characters that were used when it wasinitially constructed. E.g., the following program produces unpredictableoutput, but when thehello array is defined as a static array it showshello: #include <string_view> #include <iostream> using namespace std; string_view fun() { char hello[] = "hello"; return { hello }; } int main() { string_view obj = fun(); cout << obj << '\n'; }Thestd::string_view class provides the same members asstd::string,except for members extending thestring_view's characters (neitherappending nor inserting characters is possible). However,string_viewobjectscan modify their characters (using the index operator oratmember).
Thestring_view class also offers some extra members:
remove_prefix(size_t step):step positions.remove_suffix(size_t step):step positions.constexpr string_view operator""sv(char const *str, size_t len):string_view object containinglen characters ofstr.Likestd::string thestd::string_view class provides hashingfacilities, sostring_view objects can be used as keys in, e.g.,mapcontainers (cf. chapter12).