STL: String size and capacity)

Source: Internet
Author: User

Strings has three "sizes ":

1. Size () and length ()

Returns the number of characters in string. The two functions are equivalent.

Empty () is used to check whether the number of characters is 0, that is, whether the string is null. You should give priority to this function because it is faster than length () or size () bytes.

That is to say, use if (S. Empty () = true) instead of IF (S. Size () = 0) (note)

2. max_size ()

This function returns the maximum number of characters that a string can contain. A string usually contains all characters in a separate memory block, so it may be related to the local restrictions on PC machines. The return value is generally the maximum value of the index type minus 1. There are two reasons for "1 reduction:(A) The maximum value itself is NPOs; (B) in the specific implementation, you can easily add a '\ 0' after the internal buffer ', to use this string as a C-string (for example, through c_str ()). Once an operation function uses a string with a length greater than max_size (), the length_error exception is thrown.

3. Capacity ()

The maximum number of characters that a string can contain before memory reallocation.

It is important to make the string have enough capacity for two reasons:

1. reallocation will invalidate all references, pointer, and iterators pointing to the string.

2. reallocation is time consuming.

Therefore, if the program needs to use the references, pointers, and iterators that point to the string (or its internal characters. Or the capacity issue must be considered if the execution speed is fast. The member function reserve () is used to avoid redistribution. Reserve () allows you to reserve a certain amount of capacity and ensure that the reference remains valid when the capacity is sufficient:

STD: String s; // create empty string

S. Reserve (80); // reserve memory for 80 characters

The concept of capacity is the same for string and vector, but there is a significant difference: in the face of string, you can call reserve () to reduce the actual capacity, while the reserve () of Vector () however, this function is not available. Calling reserve () with a parameter "less than the existing capacity" is actually a non-mandatory request (nonbinding shrink request) -- if the parameter is smaller than the existing number of characters, this request is considered as a nonbinding shrink-to-fit-requset ). That is to say, you may want to reduce the capacity to a certain target, but it is not guaranteed that you can do so. The default value of the Reserve () parameter of string is 0. Therefore, when reserve () is called and no parameter is given, it is a "non-mandatory moderate reduction request ":

S. Reserve (); // "wocould like to shrink capacity to fit the current size"

Why is the scale-down action not mandatory? Because "how to get the best performance" is defined by the implementer. There may be different designs for how to handle the relationship between speed and memory consumption when implementing string. Therefore, any real work can increase the capacity with a large amount of vigor and never scale down.

C ++ Standard specifies that capacity can be reduced only when the corresponding reserve () call is used. Therefore, even if a "character is deleted or changed" occurs, any other character must be before the "operated character, the references, pointer, and iterator pointing to them remain valid.

Remarks: This article is excerpted from C ++ Library (translated by Hou Jie Meng Yan)

STL: String size and capacity)

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.