Knowledge Dump

Strings

Here, we cover some basics about the implementation and use of strings (and character arrays) in C++ (version 17). The main focus will be on the standard 1 byte char type characters.

Contents

String literals
When defining single characters, character literals are used, which is simply a character encased in single quotation marks, e.g. 'a'. It's default data type is const char. In order to assign several characters at once for arrays and string objects, string literals are employed, which are const char type arrays. They are defined by encasing text in double quotation marks, e.g. "This is a string literal." and can contain any character, besides quotation marks " and backslashes \. The size of string literals is the number of characters + 1, since they are always terminated by a special null character '\0', indicating the end of the text.
String literals can contain special characters, as long as they're encoded as escape sequences. Some notable ones include:
Escape Sequence Description
\\ Backslash sign \.
\" Double quotation mark ".
\' Single quotation mark ', only required for character literals.
\0 Terminating null character. Used to signify end of a character string.
\n New-line character.
\f New page character.
\b Backspace character.
\t Horizontal tab character.
\v Vertical tab character.
\? Question mark (see trigraph explanation below).
Note that the way some of these are handled, depends on where they are used/printed. For example, when printing to the console, the output may vary depending on the console used.

Even though single question marks can be written without an escape sequence, using two or more of them might lead to problems with trigraphs. As the name suggests, these are sequences of three characters (the first two being question marks) that are replaced by another sign. This even includes string literals and comments, which might lead to highly undesirable results. For example, the string literal "??/" would be parsed as "\", resulting in a compiler error. Other cases might not lead to outright errors, but still be problematic: "Date: ??/??/????" would be parsed as "\\????", yielding the character string \????.
Since trigraphs are often used unintentionally, many compilers throw warnings or don't parse them by default, unless they're specifically instructed to do so.

A simple way to avoid bothering with escape sequences is using raw string literals. Their syntax is R"delimiter(text goes here)delimiter", i.e. an R, followed by the text encapsulated in quotation marks, delimiter terms and parentheses. The delimiter term can be left out or any character sequence up to 16 signs, excluding parentheses, whitespaces and backslashes. Raw string literals can contain any character sequence, as long as it doesn't contain ) followed by the delimiter term, i.e. we could write R"asdf(\(OwO)/""'"??/)asdf" for the string \(OwO)/""'"??/, without the escape sequences or trigraph being processed.

Lastly, it be should mentioned that string literals are stored statically, which means they are only deleted upon program termination. Hence, for example when using them as function return, one can assign pointers to them without having to worry about the content being deleted.

Character arrays vs. string class
While the C language only supports character arrays as strings, which are thus often called C strings, C++ also supports the alternative string class. They are fundamentally different in implementation:
C-strings/Character arrays are just that – arrays of characters, which require you to set their size and make sure not to exceed it, when writing its content. They do not come with built-in functions, since it's not a class, but there is a C library header that contains several functions to manipulate/read them, called <cstring> or "string.h". One should make sure not to mix those up with the C++ header <string>. Note that the text end in such arrays is always to be marked, by adding a special \0 character. It is automatically added, when defining character arrays with string literals.
C++ strings on the other hand are, as mentioned before, class objects with dynamic size management and built-in member functions. Their size is saved inside each string object and automatically adjusted, when manipulating them. To use them, one has to include the C++ library header <string>.

The dynamic size management makes the string class much easier and safer to handle than simple character arrays. Which one is more efficient to use, depends on the operation and other factors, though. Generally, character arrays are more simply structured, but don't carry the information of their size, which has to be calculated in an O(N) operation. Checking the size of C++ strings is generally faster, since the value can simply be read in an O(1) operation.

C string header
The C string library header mostly contains functions for manipulating and reading null-terminated C-strings. Applying them on character arrays without a null character '\0' can lead to undefined behavior. The header can be included with the command #include <cstring> or alternatively #include "string.h". The following functions are contained:
  • strlen
    Declaration(s): std::size_t strlen(const char*);.
    Description: Counts the characters in a C string up to the null character ('\0' not included).
    Example: std::size_t foo = std::strlen("asdf"); (foo is 4).

  • strchr
    Declaration(s): const char* strchr(const char*, int); char* strchr(char*, int);.
    Description: Searches for the first occurrence of a character in a C string and returns a pointer to it. If the character is not found, a null pointer is returned. Even though it is specified as an integer, the second input argument is the character that is searched for – the integer will be transformed to a character via static_cast<char> (the use of int instead of char for standard library functions has its origin in the C language).
    Example: char *foo = std::strchr("asdfdsa", 'd'); (*foo is 'd', foo pointing to the third character of the string literal).

  • strrchr
    Declaration(s): const char* strrchr(const char*, int); char* strrchr(char*, int);.
    Description: Same as strchr, except that the function searches for the last occurrence of the character in a C string.
    Example: char *foo = std::strrchr("asdfdsa", 'd'); (*foo is 'd', foo pointing to the fifth character or the string literal).

  • memchr
    Declaration(s): const void* memchr(const void*, int, std::size_t); void* memchr(void*, int, std::size_t);.
    Description: Very similar to strchr, except that the character search isn't restricted by the null character '\0', but only by a specified size argument. The function takes a pointer to an object (which is possibly converted to a void pointer) and searches it for a character, which is specified in the int argument. This argument is transformed into an unsigned char. The last argument specifies the amount of characters/bytes that are to be searched for the desired character. If it's bigger than the size of the input pointer's underlying object, this leads to undefined behavior, unless the desired character is found in the object. When the searched character is found, a void type pointer to it is returned, otherwise a null pointer (NULL).
    Example: char *foo = (char*)std::memchr("ab\0cAfsga", 'A', 9); (foo is a pointer to the 'A' character in the string literal. strchr would have returned a null pointer).

  • strcmp
    Declaration(s): int strcmp(const char*, const char*);.
    Description: Takes two C strings and compares them lexicographically. The function works by comparing the ASCII number (or Unicode) equivalents of the string characters, so it can also be applied to C strings with special characters like brackets, punctuation, etc.. Returns a negative value if the first C string comes before the second (e.g. "apple" vs. "banana"), 0 if they are equal or a positive one, if it comes after the second C string (e.g. "cheese" vs. "banana"). The exact value when positive/negative may depend on the compiler used. Note that any uppercase letter (ASCII equivalent 65-90) comes before any lowercase letter (97-122). To use the function for alphabetical sorting, the words should first be transformed to all lower- or uppercase letters.
    Example: int foo = std::strcmp("/asdf", "*asdf"); (foo is positive, since '/' translates to 47, '*' to 42 and 47>42).

  • strncmp
    Declaration(s): int strncmp(const char*, const char*, size_t);.
    Description: Same as strcmp, except that it also takes a size variable that specifies up to how many characters are to be compared.
    Example: int foo = std::strncmp("aaaa", "aaab", 3); (foo is 0, since the first three characters are equal).

  • memcmp
    Declaration(s): int memcmp(const void*, const void*, std::size_t);.
    Description: Mostly like strncmp, except that the comparison doesn't end upon encountering the terminating null character in the input arguments.
    Example: int foo = std::memcmp("ab\0cd", "ab\0gh", 5); (foo is negative, since 'c' translates to 99, 'g' to 103 and 99<103. Note that strncmp would have returned 0).

  • strcoll
    Declaration(s): int strcoll(const char*, const char*);.
    Description: Similar to strcmp, except that it takes into account the collation order, set through the local LC_COLLATE value (by default, this one is set to a general "C" locale). Returns negative values if the first strings comes before the second, 0 if they are equal and positive ones if it comes after.
    Example: int foo = std::strcoll("a", "z"); (foo is negative, e.g. -25).

  • strspn
    Declaration(s): size_t strspn(const char*, const char*);.
    Description: Returns the maximum span of the substring in the first C string, which only contains characters from the second one.
    Example: std::size_t foo = std::strspn("abaacababa", "ab"); (foo is 4.)

  • strcspn
    Declaration(s): size_t strspn(const char*, const char*);.
    Description: Same as strspn, except that the span returned is of the characters not contained in the second C string (complementary span).
    Example: std::size_t foo = std::strcspn("hello world", " "); (foo is 5.)

  • strpbrk
    Declaration(s): const char* strpbrk(const char*, const char*); char* strpbrk(char*, const char*);.
    Description: Searches for the first occurrence of a character in the first C string, which is also contained in the second C string (containing the "break" characters). Returns a pointer to it, hence the function name "string pointer break". If no character matches, a null pointer is returned.
    Example: const char *foo = std::strpbrk("hello world", " "); (foo is pointer to " world", *foo is ' ').

  • strstr
    Declaration(s): const char* strstr(const char*, const char*); char* strstr(char*, const char*);.
    Description: Takes two C strings and searches the first one for occurrences of exact substring matches with the second C string. If present, a pointer is returned to the first match, pointing to the first character of the substring in the first C string. In the case of no match, a null pointer is returned. Note that the function is analogous to strpbrk, if the second C string is just one character.
    Example: const char *foo = std::strstr("hello world", "world"); (foo is pointer to "world", *foo is 'w').

  • strtok
    Declaration(s): char* strtok(char*, const char*);.
    Description: This function is built to be used multiple times on a C string, cutting it into several substrings (tokens) in the process. The initial call uses above arguments, while subsequent calls (picking up where the function stopped last) are done by replacing the first C string with NULL (null pointer declared in the header).
    On the initial call, the first C string is searched for characters not matching the characters from the second one. In the case that the first C string starts off with characters occurring in the second, those are simply skipped – both in the initial, and subsequent calls.
    • If all characters are not matching, a pointer to the first C string is returned. A subsequent function call returns a null pointer.
    • When all characters are matching, a null pointer is returned and the first string remains unchanged.
    • Upon encountering a matching character after a sequence of non-matching ones, the matching character is replaced by a terminating null character and a pointer is returned to the newly formed C string (pointing to the first non-matching character). If the function is called again with the null pointer argument, it'll start off after the returned substring.


  • strcpy
    Declaration(s): char* strcpy(char*, const char*);.
    Description: Copies the C string from the second argument into the character array from the first argument, including the null character '\0' and returns a pointer to the newly copied C string. The array has to be big enough for the copied C string, including the null character, and the memory used by the arguments shall not overlap, otherwise the function behavior is undefined.
    Example: char foo[6]; std::strcpy(foo, "hello"); (foo is "hello").

  • strncpy
    Declaration(s): char* strncpy(char*, const char*, std::size_t);.
    Description: Same as strcpy, except that the function also takes a size argument, determining the amount of characters to be copied. If the size argument is smaller than the size of the C string to be copied, the null character won't be included, resulting in a malformed C string, unless the first character array already had a terminating null that wasn't overwritten. In the case that the size argument is bigger than the C string to be copied, the remaining characters will be filled with terminating null characters.
    Example: char foo[6] = "aaaaa"; std::strncpy(foo, "hello", 3); (foo is "helaa"; not malformed, because it already had a null character).

  • memcpy
    Declaration(s): void* memcpy(void*, const void*, std::size_t);.
    Description: Just like strncpy, this function copies the object from the second argument to the first one. However, the input is not treated as C strings here, but rather as bytes of (unsigned) characters. Thus, it is unaffected by the terminating null sign and copies as many bytes, as specified in the third input argument. If said size argument is bigger than the objects to be copied or the first object is smaller than the amount of bytes copied, the function behavior is undefined. Other sources of undefined behavior are overlapping memory blocks of the first and second argument and copying of a non-trivially copyable (e.g. if the object has a non-trivial constructor/destructor) object. Lastly, neither input pointers can be null pointers. Just like with the other copy functions, the returned value is a pointer to the object that is copied to (with the notable difference that it is a void pointer, not a char one).
    Example: char foo[sizeof("hello\0 world")]; std::memcpy(foo, "hello\0 world", sizeof("hello\0 world")); (foo is "hello\0 world". Note that the terminating null character doesn't affect the copying process, unlike with strcpy or strncpy).

  • memmove
    Declaration(s): void* memmove(void*, const void*, std::size_t);.
    Description: Another copying function, very similar to memcpy. However, this one potentially allows for copying of sub-objects, e.g. overlapping arrays, since the implementation allows for indirect copying of the second argument into a memory block, before moving it to the first argument. Whether indirect copying is actually used, may vary with input, though, depending on necessity and the size of the object to copy. Applying the function on non-trivially copyable or insufficiently big objects (smaller than the 3rd size argument) still results in undefined behavior.
    Example: char foo[20] = "Hello World"; std::memmove(foo+6, foo, 11); (foo is "Hello Hello World"; if memcpy had been used instead, the result would've been "Hello Hello Wollo").

  • strcat
    Declaration(s): char* strcat(char*, const char*);.
    Description: The function takes two C string arguments and concatenates (appends) the second one to the first. The null character of the first argument is overwritten by the second, so the outcome is one C string. Note that insufficient array size to accommodate the second in the first C string, causes undefined behavior. Same goes for overlapping memory of the input arguments.
    Example: char foo[20]="hello "; std::strcat(foo, "world"); (foo is "hello world").

  • strncat
    Declaration(s): char* strncat(char*, const char*, std::size_t);.
    Description: Just like strcat, except that the function also has a size argument indicating the maximum amount of concatenated characters. Unlike with strncpy, no additional null characters are appended, if the specified number of characters to copy is larger than the second C string.
    Example: char foo[20]="hello "; std::strncat(foo, "world", 2); (foo is "hello wo").

  • strxfrm
    Declaration(s): std::size_t strxfrm(char*, const char*, std::size_t);.
    Description: Somewhat obscure function, that works similarly to strncpy in the way that it copies a C string from the second argument to the first, with the length being specified in the third argument. However, the copied C string is also transformed to be comparable with strcmp, without having to use strcoll, i.e. it accounts for the collation order determined by the LC_COLLATE value. The returned value is the C string length of the transformed string, excluding the null character. As with the other copy functions, if the first character array is too small to accommodate the second, or if they overlap, the behavior is undefined.
    Example: char foo[20]; std::strxfrm(foo, "äaÜuöoéeè", 9); (foo contains "äaÜuöoéeè", possibly transformed according to LC_COLLATE value).

  • memset
    Declaration(s): void* memset(void*, int, std::size_t);.
    Description: Fills a memory block, specified by the void pointer, with a character. The character is passed by the int argument and subsequently converted in the function body. The amount of bytes that shall be written is declared in the size argument. The function will produce undefined behavior, if the size argument is larger than the object pointed to by the void pointer, if the character that shall fill the memory block is contained in said block, or if the object is not trivially copyable (e.g. has a non-trivial constructor/destructor). The returned void pointer is the same as the one from the first input argument.
    Example: char foo[20] = "hello world"; std::memset(foo, 'a', 5); (foo is "aaaaa world").

  • strerror
    Declaration(s): char* strerror(int);.
    Description: This function takes in an integer, specifying an error code number and returns a C string of the corresponding message. To get the latest error code number, the C header <errno> ("errno.h") can be used. It contains an integer macro named errno, saving the error number of the latest error. The actual error messages to each number are defined by the locale value LC_MESSAGES.
    Example: char err[100]; std::strcpy(err, std::strerror(1)); (err is "Operation not permitted" in the language set by LC_MESSAGES).


Besides these functions, the header also includes a null pointer macro (NULL) and the size type size_t, which is similar to unsigned integers.

C++ string header
The C++ equivalent of the C string header is called string and can be used with #include <string>. It contains several string classes for different character types, with sizes 1 Byte (standard char), 2 Byte, 4 Byte or "wide characters", as well as a class template for implementation of specialized string classes. Conversion and iterator template functions are included, too.

While the string class is the most commonly used of the C++ string header, it's actually just an instance of the template class basic_string, which is declared as basic_string< class charT, class traits = char_traits<charT>, class alloc = allocator<charT> >.
Using this template with the four commonly used character types and the default character trait and allocator arguments, yields the four string class instances already included in the header (with a typedef name equivalent):
  • basic_string<char>: Covers char character type strings, defined as the string class.
  • basic_string<char16_t>: Class for 2 byte characters char16_t, with typedef name u16string.
  • basic_string<char32_t>: 4 byte character string class, named u32string.
  • basic_string<wchar_t>: Wide character class, equivalent to wstring.
The template includes plenty member functions, most of which are listed (in their string instantiated versions, not all overloads included) below:
  • size / length
    Declaration(s): std::size_t size() const noexcept; / std::size_t length() const noexcept;.
    Description: Identical functions, which return the amount of characters stored in the string. Note that this doesn't correspond to the amount of memory space needed to store these characters for multi-byte character types.

  • empty
    Declaration(s): bool empty() const noexcept;.
    Description: Checks, whether the string is empty.

  • clear
    Declaration(s): void clear() noexcept;.
    Description: Deletes all characters from string.

  • max_size>
    Declaration(s): std::size_t max_size() const noexcept;.
    Description: Returns the maximum character length a string might potentially reach, considering given information from the system and/or implementation. The actual length limit might be lower, though.

  • resize
    Declaration(s): void resize(std::size_t); void resize(std::size_t, char);.
    Description: Resizes the string to the specified amount of characters. If it is lower than the current size, the string is shortened – otherwise it's lengthened by adding null characters until the length is met. If a character argument is added, it is used to fill up the string instead of null characters. Note: This character must be of the same base type, as the string class used, e.g. a wstring's method requires a wchar_t character.

  • capacity
    Declaration(s): std::size_t capacity() const noexcept;.
    Description: Returns the maximum amount of characters, which can currently be accommodated in the string. It is automatically increased, when the string is expanded beyond this length, often by doubling the old capacity (in order to avoid frequent resizing operations).

  • shrink_to_fit
    Declaration(s): void shrink_to_fit();.
    Description: Using this command, excess memory allocated to the string can be freed, to exactly fit its length. However, this request is non-binding and may have no effect (implementation dependent). Note: This

  • reserve
    Declaration(s): void reserve (std::size_t);.
    Description: This function is normally used to allocate memory space for a string, which is soon to be notably increased in size, to avoid multiple allocator calls for resizing operations. If the specified size is less than the current string capacity, the memory allocated might be reduced (non-binding shrink request). In the case that it's smaller than the string length, it's equivalent to a shrink_to_fit command. (This might change in C++20)

  • c_str / data
    Declaration(s): const char* c_str() const noexcept; / const char* data() const noexcept;.
    Description: Identical functions, which return a pointer to the actual character data contained in the string, in the form of a C string. Note that this data might be subject to reallocation, when some non-const member functions are called, which invalidates all pointers and references to the old memory address!

  • copy
    Declaration(s): std::size_t copy (char* dest, std::size_t count, std::size_t position = 0) const;.
    Description: Copies (a part of) the contents of the string to the character array specified in the first argument "dest". The count argument sets the amount of characters to be copied, while the optional third argument can be used to determine after how many characters the contents are to be copied (defaults to 0, i.e. start of the string). The function returns the number of actually copied characters, which may differ from the second argument, if it surpasses the number of characters that can be copied from the string at the specified position.

  • find
    Declaration(s): std::size_t find(const std::string& str, std::size_t position = 0) const noexcept;
    std::size_t find(const char* cstr, std::size_t position = 0) const;
    std::size_t find(char c, std::size_t position = 0) const noexcept;.
    std::size_t find(const char* char_array, std::size_t position, std::size_t count) const;
    Description: The first three variants search the string for occurrences of another string, C string or character after the specified position (the default 0 signifies the start of the string). The fourth takes in a character array of length count and searches the string after "position" characters, without a default value. All functions return the position of the first occurrence, if the search was successful. When no match is found, the member constant std::string::npos is returned, which is the maximum value of std::size_t (equivalent to -1, since it's an unsigned type).

  • rfind
    Declaration(s): See find (input argument overloads identical).
    Description: Exactly the same as find, except that the search begins at the end of the string and the position argument offset is reversed accordingly (e.g. if position = 1, the search starts at the second last character). The value returned is the first character of the last match in the string, or std::string::npos in case of no occurrence.

  • find_first_of
    Declaration(s): See find (input argument overloads identical).
    Description: Equivalent to find, except that the search is for all characters specified in the input individually, not the term as a whole. For example, an input of "hello" searches for the first occurrence of any of the characters 'h', 'e', 'l', 'o'.

  • find_last_of
    Declaration(s): See find (input argument overloads identical).
    Description: Reversed search order equivalent of find_first_of.

  • find_first_not_of
    Declaration(s): See find (input argument overloads identical).
    Description: Similar to find_first_of, except that it searches for the first character of the string, which is not contained in the input term.

  • find_last_not_of
    Declaration(s): See find (input argument overloads identical).
    Description: Reversed search order equivalent of find_first_not_of.

  • substr
    Declaration(s): std::string substr (std::size_t position = 0, std::size_t length = std::string::npos) const;.
    Description: Copies (a part of) the string into a new one. The first argument specifies an offset, after which point the string characters are to be copied (defaults to 0, i.e. the start of the string). The second sets the maximum length of the substring, which is also limited by the amount of characters that can actually be copied from the string. It defaults to std::string::npos (highest possible value of std::size_t), i.e. if the function is called with only one argument, the substring contains all characters after the input position offset. When called with no arguments, a copy of the string is returned.

  • compare
    Declaration(s): int compare(const std::string& str) const noexcept;
    int compare(std::size_t position, std::size_t length, const std::string& str) const;
    int compare(std::size_t position, std::size_t length, const std::string& str, std::size_t subpos, std::size_t sublen = std::string::npos) const;
    int compare(const char* cstr) const;
    int compare(std::size_t position, std::size_t length, const char* cstr) const;
    int compare(std::size_t position, std::size_t length, const char* arr, std::size_t count) const;.
    Description: Compares the string (or a substring of it) lexicographically to another string, C string or character array with set size. When present, the first two size arguments determine which part of the string is to be compared: The first specifies an offset, with 0 meaning the comparison starts with the first character and the second one sets how many string characters are to be compared (std::string::npos for comparison of whole string after offset). If the comparison is with another string, two more arguments setting the offset and length for it can be specified.
    Comparisons with char arrays, without a set count argument determining its length, require a terminating null sign in the array (C string).
    The outcome is returned as an integer. It is 0, if the compared strings are equal, negative, if the string calling the function comes before the object in the arguments, or positive if it comes after (in lexicographical order).

  • operator==, operator!=, operator< etc. (Relational operators)
    Declaration(s): bool operator== (const std::string& str1, const std::string& str2) noexcept;
    bool operator== (const char* cstr, const std::string& str);
    bool operator== (const std::string& str, const char* cstr);, etc. .
    Description: Operator for lexicographical comparison of two strings, or a string with a C string. Works by calling compare and returning the relation value as a bool.

  • push_back
    Declaration(s): void push_back(char);.
    Description: Appends a single character at the end of the string.

  • pop_back
    Declaration(s): void pop_back();.
    Description: Removes a single character from the end of the string. Needs to be used with care, since calling it on an empty string leads to undefined behavior.

  • append
    Declaration(s): std::string& append(const std::string& str);
    std::string& append(const std::string& substr, std::size_t offset, std::size_t length = std::string::npos);
    std::string& append(const char* cstr);
    std::string& append(const char* arr, std::size_t count);
    std::string& append(std::size_t count, char c).
    Description: Appends either a (sub-)string, C string, character array (with size argument) or a repeated character to the end of the string. The returned reference is the string itself.

  • operator+=
    Declaration(s): std::string& operator+= (const std::string& str);
    std::string& operator+= (const char* cstr);
    std::string& operator+= (char c);.
    Description: Operator for appending a string, C string or a single character.

  • operator+
    Declaration(s): std::string operator+ (const std::string& str1, const std::string& str2);
    std::string operator+ (const std::string& str, const char* cstr);
    std::string operator+ (const char* cstr, const std::string& str);
    std::string operator+ (const std::string& str, char c);
    std::string operator+ (char c, const std::string& str);.
    Description: Operator that concatenates two strings, a string and a C string, or a string and a character, to form a new string.

  • assign
    Declaration(s): See append (input argument overloads identical).
    Description: Assigns new content to the string, deleting the old. Input variants are a (sub-)string, C string, character array (with size argument) or a repeated character.

  • insert
    Declaration(s): std::string& insert(std::size_t position, const std::string& str);
    std::string& insert(std::size_t position, const std::string& substr, std::size_t offset, std::size_t length = std::string::npos);
    std::string& insert(std::size_t position, const char* cstr);
    std::string& insert(std::size_t position, const char* arr, std::size_t count);
    std::string& insert(std::size_t position, std::size_t count, char c);.
    Description: Similar to append, except that it adds the input (sub-)string / C string / character array / repeated character at the specified position (with 0 meaning the start of the string). The returned reference is the string itself.

  • erase
    Declaration(s): std::string& erase (std::size_t offset = 0, std::size_t count = std::string::npos);.
    Description: Removes count characters after the specified offset. With no arguments, the whole string content is deleted. When only the offset is specified, all characters after the offset are erased. The returned value is again the string itself (*this).

  • replace
    Declaration(s): std::string& replace(std::size_t position, std::size_t count, const std::string& str);
    std::string& replace(std::size_t position, std::size_t count, const std::string& str, std::size_t offset, std::size_t length = std::string::npos);
    std::string& replace(std::size_t position, std::size_t count, const char* cstr);
    std::string& replace(std::size_t position, std::size_t count, const char* arr, std::size_t arr_length);
    std::string& replace(std::size_t position, std::size_t count, std::size_t repeat_count, char c);.
    Description: Combination of erase and insert. First deletes count characters at the specified position (0 being the beginning of the string) and inserts the input (sub-)string / C string / character array / repeated character instead. Returned reference is *this.

  • swap
    Declaration(s): void swap(std::string&);.
    Description: Swaps the contents of the string with the one's from the input string (both are changed). Note: This function is just an overload of std::swap.

  • front
    Declaration(s): char& front(); const char& front() const;.
    Description: Returns a reference to the first character of the string content, which is const if the string is const. Calling this member function with an empty string leads to undefined behavior.

  • back
    Declaration(s): char& back(); const char& back() const;.
    Description: Same as front, except that it returns a reference to the last string character.

  • at
    Declaration(s): char& at(std::size_t position); const char& at(std::size_t position) const;.
    Description: Same as front, except that it returns a reference to the character at "position" (0 being the first character).

  • operator[]
    Declaration(s): char& operator[] (std::size_t position); const char& operator[] (std::size_t position) const;.
    Description: Operator, equivalent to at.

  • operator<<
    Declaration(s): std::ostream& operator<< (std::ostream&, const std::string&);.
    Description: Overload of the output stream operator for strings. The contents of the string are inserted into the ostream object.

  • operator>>
    Declaration(s): std::istream& operator>> (std::istream&, std::string&);.
    Description: The ">>" operator for input streams is overloaded for strings. The extracted characters are saved in the string object, overwriting its old contents in the process. Whitespaces in the input are used as delimiters, resulting in only one "word" per string – when this is not desired, getline can be used (see below).

  • getline
    Declaration(s): istream& getline (istream& is, string& str); std::istream& getline (std::istream&, string&, char);.
    Description: Similar to the >> operator, except that the delimiter defaults to the newline character '\n'. Another delimiter can be chosen in a third char argument.


Additionally, there are several functions returning predefined iterators, which offer the usual functionalities. They can have reversed increment direction, be constant iterators (i.e. dereferenced content can't be changed) or both. Note: Some starting points of the iterators aren't directed at actual characters in the string (such as end()) and shall not be dereferenced.
Name Starting point Increment direction Constant iterator
begin First character Normal No
end Theoretical character after the last character Normal No
rbegin Last character Reversed No
rend Theoretical character before the first character Reversed No
cbegin First character Normal Yes
cend Theoretical character after the last character Normal Yes
crbegin Last character Reversed Yes
crend Theoretical character before the first character Reversed Yes