C++ string handling
The C++ programming language has support for string handling, mostly implemented in its standard library.[src] The language standard specifies several string types, some inherited from C, some designed to make use of the language's features, such as classes and RAII.[src] The most-used of these is , while is used for non-owning string views.[src] Since the initial versions of C++ had only the "low-level" C string handling functionality and conventions, multiple incompatible designs for string handling classes have been designed over the years and are still used instead of , and C++ programmers may need to handle multiple conventions in a single application.[src]
The C++ programming language has support for string handling, mostly implemented in its standard library.[src] The language standard specifies several string types, some inherited from C, some designed to make use of the language's features, such as classes and RAII.[src] The most-used of these is , while is used for non-owning string views.[src] Since the initial versions of C++ had only the "low-level" C string handling functionality and conventions, multiple incompatible designs for string handling classes have been designed over the years and are still used instead of , and C++ programmers may need to handle multiple conventions in a single application.[src] ==History== The type is the main string datatype in standard C++ since 1998, but it was not always part of C++.[src] From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.[src] In modern standard C++, a string literal such as still denotes a NUL-terminated array of characters.[src] Using C++ classes to implement a string type offers several benefits of automated memory management and a reduced risk of out-of-bounds accesses, and more intuitive syntax for string comparison and concatenation.[src] Therefore, it was strongly tempting to create such a class.[src] Over the years, C++ application, library and framework developers produced their own, incompatible string representations, such as the one in AT&T's Standard Components library (the first such implementation, 1983) or the type in Microsoft's MFC or the in Qt.[src] The various vendors' string types have different implementation strategies and performance characteristics, which made conversion of code and even assignment from one to another difficult: Some used a copy-on-write strategy with major performance implications (making some operations much faster and some much slower).[src] Though most agreed on syntax for comparison, assignment, and concatenation; the syntax to extract substrings or perform searches varied widely.[src] Not everything was Unicode, leading to widespread variations in how or if encoding was stored and when translation was done, and sometimes very expensive assignment operators.[src] It was also believed code units larger than bytes were necessary to support Unicode, leading to string types that could not interoperate in any efficient manner.[src] While standardized strings, legacy applications still commonly contain such custom string types and libraries may expect C-style strings, making it "virtually impossible" to avoid using multiple string types in C++ programs and requiring programmers to decide on the desired string representation ahead of starting a project.[src] In a 1991 retrospective on the history of C++, its inventor Bjarne Stroustrup called the lack of a standard string type (and some other standard types) in C++ 1.0 the worst mistake he made in its development; "the absence of those led to everybody re-inventing the wheel and to an unnecessary diversity in the most fundamental classes".[src] ==Description== The class is the standard representation for a text string since C++98.[src] The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining substrings.[src] A can be constructed from a C-style string, and a C-style string can also be obtained from one.[src] The individual units making up the string are of type char (a single byte).[src] In modern usage these are often not "characters", but parts of a multibyte character encoding such as UTF-8.[src] import std; using std::string; int main() { string foo = "fighters"; string bar = "stool"; if (foo != bar) { std::println("The strings are different!"); } foo += bar + " end"; // foo is now "fightersstool end" std::println("{}", foo); // print } ===Copy on Write=== A copy-on-write implementation means string a = "hello!"; string b = a; // Copy constructor does not actually copy the content of to ; instead, both strings share their contents and a reference count on the content is incremented.[src] The actual copying is postponed until a mutating operation, such as changing a character, makes the strings' contents differ.[src] The copy-on-write strategy was deliberately allowed by the initial C++ Standard for because it was deemed a useful optimization, and used by nearly all implementations.[src] However, there were mistakes, in particular the operator[] returned a non-const reference in order to make it easy to port C code such as s[5] = 'a'; , and had to trigger a copy.[src] Multi-threading caused optimizations (such as not copying when the reference count is one) to fail and on modern processors the necessary locking to examine or change the reference count is more expensive than copying a small string.[src] This caused implementations, first MSVC and later GCC, to move away from copy-on-write.[src] The optimization was finally disallowed in C++11.[src] Most implementations now do small string optimization (SSO), where strings less than 16 (or 23) bytes long are stored in the string object and no buffer is allocated.[src] This is significantly faster than reference counting as many strings are this small, and buffer allocation is extremely expensive compared to copying local blocks of memory.[src] import std; using std::string; void outputString(string s) { std::print("{}", s); } // ...[src] string s = "..."; outputString(s); // makes a copy of s outputString("this is a literal string"); // copies the string, sometimes twice Though the compiler could optimize this away for inline functions, few relied on this and almost always strings are passed as a const reference: Conversion from a string constant also required constructing a temporary and was slow, usually leading to overloaded functions: import std; using std::string; void outputString(const string& s) { std::print("{}", s); } void outputString(const char* s) { std::print("{}", s); } // ...[src] string s = "..."; outputString(s); // does not copy s, passes a pointer/reference outputString("this is a literal string"); // calls overload and passes raw pointer The C++17 standard adds the class that is only a pointer and length to read-only data, and is a drop-in replacement for an immutable temporary and makes pass-by-value arguments faster than either of the above examples: import std; using std::string; using std::string_view; void outputString(string_view sv) { std::print("{}", s); } // ...[src] string s = "..."; outputString(s); // one less level of indirection outputString("this is a literal string"); // furthermore compiler may optimize away need to use strlen() All functions in a library that take a string argument must be rewritten to take advantage of this in C++17, as conversion from to is still expensive.[src] namespace std { using string = basic_string ; } There is a similar class , which consists of wide characters ( wchar_t ), and is most often used to store UTF-16 text on Windows and UTF-32 on most Unix-like platforms.[src] The C++ standard, however, does not impose any interpretation as Unicode code points or code units on these types and does not even guarantee that a wchar_t holds more bits than a char .[src] To resolve some of the incompatibilities resulting from wchar_t 's properties, C++11 added two new classes: and (made up of the new types char16_t and char32_t ), which are the given number of bits per code unit on all platforms.[src] C++11 also added new string literals of 16-bit and 32-bit "characters" and syntax for putting Unicode code points into null-terminated (C-style) strings.[src] A is guaranteed to be specializable for any type with a struct to accompany it.[src] As of C++11, only char , wchar_t , char16_t and char32_t specializations are required to be implemented.[src] In , the two template parameters and have the default values of and by default.[src] A is also a Standard Library container, and thus the Standard Library algorithms can be applied to the code units in strings.[src] "The quick brown fox" , of type const char[] L"The quick brown fox" , of type const wchar_t[] u8"The quick brown fox" , of type const char8_t[] u"The quick brown fox" , of type const char16_t[] U"The quick brown fox" , of type const char32_t[] For each of these, a raw string literal is also provided (with the R written the furthest right of the prefix, such as R"The quick brown fox" , LR"The quick brown fox" , uR"The quick brown fox" , etc.[src] These raw string literals do not escape any character.[src] Note that in C++, a string literal "foo" has type const char[] , while in C this same string literal has type char[] , without const -qualification.[src] C++ has string literals for objects using the std::literals::string_literals::operator""s operator (which creates a std::string ) , and the std::literals::string_view_literals::operator""sv operator (which creates a std::string_view ) .[src] Note that literals , string_literals , string_view_literals are all inline namespaces.[src] using std::string; using std::string_view; using namespace std::literals; const string &shortGreeting = "Hello, world!"s; const string_view &verboseGreeting = "Salutations, universe!"sv; Language linkage specification ( extern "C" ) The empty string in a literal operator name ( operator ""foo ) Attributes ( deprecated("Reason") , nodiscard("Reason") ) Deleted function body ( = delete("Reason"); One of the first applications of this, in C++11, was to the message for static_assert .[src] This restricted any construction of the messages to what the C preprocessor could do.[src] In C++26 this was relaxed, allowing anything that can cast to a constexpr std::string_view , in particular so that could be used.[src] ==See also== String functions C++ input/output library == References==[src]
55 sentences · 55 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"C++ string handling." encyclopegia.org, 2026, https://encyclopegia.org/wiki/c-string-handling. Accessed 27 November 2026.
Chicago
"C++ string handling." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/c-string-handling.
APA
encyclopegia.org. (2026). C++ string handling. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/c-string-handling
Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON