赛派号

家用220v电压电流是多少安 C++ string handling

Program handling of character strings

C++ Standard Library Input/output Strings algorithm functional Containers Sequence containers Associative containers Unordered associative containers C standard library Data types Character classification Strings Mathematics File input/output Date/time Localization Memory allocation Process control Signals Alternative tokens Miscellaneous headers: vte

The C++ programming language has support for string handling, mostly implemented in its standard library. 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. The most-used of these is std::string.

Since the initial versions of C++ had only the "low-level" C string handling functionality and conventions, multiple incompatible designs for string handling classes he been designed over the years and are still used instead of std::string, and C++ programmers may need to handle multiple conventions in a single application.

History[edit]

The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. 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. In modern standard C++, a string literal such as "hello" still denotes a NUL-terminated array of characters.[1]

Using C++ classes to implement a string type offers several benefits of automated memory management and a reduced risk of out-of-bounds accesses,[2] and more intuitive syntax for string comparison and concatenation. Therefore, it was strongly tempting to create such a class. 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)[3] or the CString type in Microsoft's MFC.[4] While std::string standardized strings, legacy applications still commonly contain such custom string types and libraries may expect C-style strings, making it "virtually impossible" to oid using multiple string types in C++ programs[1] and requiring programmers to decide on the desired string representation ahead of starting a project.[4]

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".[3]

Implementation issues[edit]

The various vendors' string types he different implementation strategies and performance characteristics. In particular, some string types use a copy-on-write strategy, where an operation such as

string a = "hello!"; string b = a; // Copy constructor

does not actually copy the content of a to b; instead, both strings share their contents and a reference count on the content is incremented. The actual copying is postponed until a mutating operation, such as appending a character to either string, makes the strings' contents differ. Copy-on-write can make major performance changes to code using strings (making some operations much faster and some much slower). Though std::string no longer uses it, many (perhaps most) alternative string libraries still implement copy-on-write strings.

Some string implementations store 16-bit or 32-bit code points instead of bytes, this was intended to facilitate processing of Unicode text.[5] However, it means that conversion to these types from std::string or from arrays of bytes is dependent on the "locale" and can throw exceptions.[6] Any processing advantages of 16-bit code units vanished when the variable-width UTF-16 encoding was introduced (though there are still advantages if you must communicate with a 16-bit API such as Windows). Qt's QString is an example.[5]

Third-party string implementations also differed considerably in the syntax to extract or compare substrings, or to perform searches in the text.

Standard string types[edit]

The std::string class is the standard representation for a text string since C++98. The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining substrings. An std::string can be constructed from a C-style string, and a C-style string can also be obtained from one.[7]

The individual units making up the string are of type char, at least (and almost always) 8 bits each. In modern usage these are often not "characters", but parts of a multibyte character encoding such as UTF-8.

The copy-on-write strategy was deliberately allowed by the initial C++ Standard for std::string because it was deemed a useful optimization, and used by nearly all implementations.[7] However, there were mistakes, in particular the operator[] returned a non-const reference in order to make it easy to port C in-place string manipulations (such code often assumed one byte per character and thus this may not he been a good idea!) This allowed the following code that shows that it must make a copy even though it is almost always used only to examine the string and not modify it:[8][9]

std::string original("aaaaaaa"); std::string string_copy = original; // make a copy char* pointer = &string_copy[3]; // some tried to make operator[] return a "trick" class but this makes it complex arbitrary_code_here(); // no optimizations can fix this *pointer = 'b'; // if operator[] did not copy, this would change original unexpectedly

This caused implementations, first MSVC and later GCC, to move away from copy-on-write.[10] It was also discovered that the overhead in multi-threaded applications due to the locking needed to examine or change the reference count was greater than the overhead of copying small strings on modern processors[11] (especially for strings smaller than the size of a pointer). The optimization was finally disallowed in C++11,[8] with the result that even passing a std::string as an argument to a function, for example void function_name(std::string s); must be expected to perform a full copy of the string into newly allocated memory. The common idiom to oid such copying is to pass as a const reference.[12]

void function_name(const std::string& str_) { std::cout

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了