STL Source Code Anatomy # class String #

Source: Internet
Author: User

STL Source Parsing string


Recently intends to take a good look at the STL source code implementation ...


Various definitions can not find the head is big.

First of all you need a helper, ctags can't we use global (something to do it ourselves, so not introduced).


Under the path of the STL library bits/stringfwd.h you can find the definition

You will find that the standard library class string We use is essentially basic_string


The definition of class string is 2000+, and we will not give all the details here, only to analyze where the individual feels important and interesting.

You see, the designers of this class have made statements about various types of variables related to the String class, Value_type, Size_type ...




Class _rep is used to represent strings

The string class always uses _m_length + 1 characters and ensures that the last one is the end of the null character to represent the strings.

And the application of memory will always apply some more (_m_capacity), _m_capacity greater than or equal to _m_length.


Is the definition of struct _rep_base, we can see that the struct is used instead of the class, which is already in the private label, so the entire struct is privately owned.

Notice that struct _rep is a derived class of _rep_base.

And then we can see that the capacity interface of the String class is provided here capacity ()



Here the STL author writes two functions of the same interface, possibly for the convenience of the programmer to call. Size () in the same sense as length () returns the length of the current string, but does not include null characters, which are consistent with C's strlen ().


Let's write a little demo test.

#include <iostream> #include <string>using namespace Std;int main () {    string str ("Hello World");    cout << "Capacity" << str.capacity () << Endl;    cout << "Size    " << str.size () << Endl;    cout << "Length  " << str.length () << Endl;    cout << "Max_size" << str.max_size () << Endl;    return 0;}



You will see the implementation of various constructors:

      /** * @brief Construct an empty string using allocator @a A.       */Explicit basic_string (const _alloc& __a);       /** * @brief Construct string with copy of value of @a str.       * @param __str Source string.      */basic_string (const basic_string& __STR);       /** * @brief Construct string as copy of a substring.       * @param __str Source string.       * @param __pos Index of first character to copy from.       * @param __n number of characters to copy (default remainder).      */basic_string (const basic_string& __STR, Size_type __pos, size_type __n = NPOs);       /** * @brief Construct string as copy of a substring.       * @param __str Source string.       * @param __pos Index of first character to copy from.       * @param __n number of characters to copy.       * @param __a Allocator to use. */basic_string (const basic_string& __STR, Size_type __pos, Size_type __n, const _alloc& __a);       /** * @brief Construct string initialized by a character%array.       * @param __s Source character%array.       * @param __n number of characters to copy.       * @param __a Allocator to use (default is default Allocator).       * NB: @a __s must had at least @a __n characters, ' \\0 ' * has no special meaning.      */basic_string (const _chart* __s, Size_type __n, const _alloc& __a = _alloc ());       /** * @brief Construct string as copy of a C string.       * @param __s Source C string.       * @param __a Allocator to use (default is default Allocator).      */basic_string (const _chart* __s, const _alloc& __a = _alloc ());       /** * @brief Construct string as multiple characters.       * @param __n number of characters.       * @param __c Character to use.       * @param __a Allocator to use (default is default Allocator). */
It is worth mentioning that the end of our usual string class initialization is to give it a type C string, as the initialization parameters. And this is actually called here.

basic_string (const _chart* __s, const _alloc& __a = _alloc ());
Other constructors that use string class references to initialize parameters are also given.


We also saw the clear (), empty interface

Clear () is used to erase the string so that it is empty. and how Empty () is used to detect if the string is empty.



#include <iostream> #include <string>using namespace Std;int main () {    string str ("Hello");    cout << "Capacity" << str.capacity () << Endl;    cout << "Size    " << str.size () << Endl;    cout << "Length  " << str.length () << Endl;    cout << "Max_size" << str.max_size () << Endl;    if (str.empty () = = True)    {        cout << "Empty" << Endl;    }    else    {        cout << "not empty" << Endl;    }    Str.clear ();    if (str.empty () = = True)    {        cout << "Empty" << Endl;    }    else    {        cout << "not empty" << Endl;    }    return 0;}







Not finished to update ~









STL Source Code Anatomy # class String #

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.