|
C ++ string in the end is what?
To answer this question, one should first understand what is basic_string.
Look basic_string statement:
template < class charT, // type definition characters in a string
class traits = char_traits < charT>, // basic_string :: traits_type
class Alloc = allocator < charT> // basic_string :: allocator_type
> Class basic_string;
Visible, basic_string class is essentially a template.
Then explain a little more detail:
1. About char_traits
statement:
template < class charT> struct char_traits;
effect:
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 attribute of the character, and provides specific semantic role in the characters or character sequences of certain operations.
2. About allocator
statement:
template < class T> class allocator; // at < memory> header file allocator: Splitter
effect:
Allocators are classes that define memory models to be 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 part of the standard library is used, in particular STL memory model.
Now let's string declaration:
typedef basic_string < char, char_traits < char>, allocator < char >> string;
Now, we understand, the original is such a thing:
With basic types char instantiate the class template basic_string, to give a specific template class, then, is it typedef string.
In other words, is essentially a template class string is basic_string < char, char_traits < char>, allocator < char >>, string is the corresponding "short." Intuitively understand, string instance of the object (that is string str; the str) is a char sequence, but unlike char * str, stingr str with many good package for their own operations.
ps: basic_string there are other examples, such as:
typedef basic_string < wchar_t, char_traits < wchar_t>, allocator < wchar_t >> wstring; |
|
|
|