C ++ - string class of common construction and operation capacity function

1, common string class object constructor

Function name Function Description
string() Empty string class object configuration
string(const char * s) Construct with C-string string class object
string(size_t n, char c) string class object containing n characters c
string(const string&s) Copy constructor
string(const string&s, size_t n) The new class object with the string configuration the first n characters of s

2, string class object operation capacity

Function name Function Description
size_t size() const Returns a string length of valid characters
size_t length() const Returns a string length of valid characters
size_t capacity( ) const Returns the total size of space
bool empty() const Detecting the release of the string is empty string is true, false otherwise
void clear() Empty effective character
void resize(size_t n, char c) The effective number of characters into a number n, the extra space is filled with character c
void resize(size_t n) The number of valid characters into the n, the extra space is filled with a 0
void reserve(size_t res_arg=0) Reserve space for the string

Note:
1. There is no difference in the size and length size of the length of the string
2. String default capacity capacity VS environment 15, each expansion 16, the total capacity of the multiple -116, the final position is' \ 0 '. (Sequence table corresponding to the character string)
3.empty (); // empty sentence, a return is empty, non-empty return 0
4.clear (); // empty, without changing the size of the capacity of
5.reserve // expansion reserve space

string s1;
s1.reserve(500);
cout << s1.capacity() << endl;//输出为511,500之后第一个16的倍数-1

6.resize: directly change the value of size, space reset. If the value exceeds the value resize capacity, it will first call the reserve allocated space
7.reserve, resize, clear, does not result in narrow space

Published 77 original articles · won praise 23 · views 7549

Guess you like

Origin blog.csdn.net/Hots3y/article/details/100937823