What exactly is a C + + string

Source: Internet
Author: User
Tags traits

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.