What exactly is a C + + string?
To answer this question, first understand what is basic_string.
Take a look at Basic_string's statement:
class CharT, // defines the type of character in the string class traits = Char_traits<chart> // basic_string::traits_type class Alloc = allocator<chart> // basic_string::allocator_type class basic_string;
Visible, basic_string is essentially a class template.
Explain it in a little more detail:
1. About Char_traits
Statement:
Template <classstruct char_traits;
Role:
Character Traits classes Specify Character properties and provide specific semantics for certain operations on characters and sequences of characters. (from C + + REFERENCCE, Address: http://www.cplusplus.com/reference/string/char_traits/)
That is, it specifies the properties of a character and provides specific semantics for certain operations that function on a character or sequence of characters.
2. About Allocator
Statement:
Template <classclass allocator; // <memory> header file allocator: Dispenser
Role:
Allocators is classes that define memory models to being used by some parts of the standard Library, and most specifically, by STL Containers. (from C + + REFERENCCE, Address: Http://www.cplusplus.com/reference/memory/allocator/?kw=allocator)
That is, it defines some of the content used in the standard library, especially the STL's memory model.
Now let's look at the statement of string:
typedef basic_string<Char, char_traits<char;, allocator<charstring ;
Now, we understand that it turns out that this is what happened:
Instantiate the class template basic_string with the base type char, get a concrete template class, and then typedef it as a string.
In other words, string is essentially a template class, that is Basic_string<char, Char_traits<char>, allocator<char>>,string is the corresponding "abbreviation". Intuitively, the instance object of string (that is, string str; str) is a char sequence, but differs from char* Str,stingr str with many encapsulated actions for itself.
Ps:basic_string also has other examples, such as:
typedef basic_string<wchar_t, Char_traits<wchar_t>, allocator<wchar_t>> wstring;
What exactly is a C + + string