Chapt 17. std::string

17.1 std::string and std::wstring (Learn C++)

A. Base class: ‘basic_string<>’

1. std::string: used for ‘UTF-8’

2. std::wstring: used for ‘UTF-16’

*There no built-in string for ‘UTF-32’. Should extend form ‘basic_string<>’

B. Not support functionality

1. Tokenization

2. Capitalization

3. Case-insensitive comparison

4. Whitespace trimming

5. Conversion UTF-8 –> UTF-16

*You should implement above functionality yourself

17.2 std::string construction (Learn C++)

There many constructors for string class, but I’ll only cover common ones; see further

A. Constructors

1. string::string() : default empty string

2. string::string(const string& str) : copy constructor

3. string::string(const string& str, size_t pos, size_t len): substring

4. string::string(const string* str) : copy from C-string

5. string::string(const string* str, size_t n) : copy first ‘n’ characters

5. string::string(size_t n, char c) : fill ‘n’ characters

B. Source Code

17.3 std::string length and capacity (Learn C++)

A. std::string length() == std::string size() – further see

1. return # of bytes used for current contents

2. always equal or less than ‘capacity’ of string

B. std::string capacity() – further see

1. return # of bytes allocated to string

2. always equal or grater than ‘length’ of string

3. memory reallocation is comparatively expensive

C. Source Code

17.4 std::string character access and conversion to C-style arrays (Learn C++)

A. char& operator[] (size_t pos) == char& at (size_t pos)

both function returns the character with index ‘pos’

return type is reference;’char &’. so can edit the character

B. const char* c_str()

return C-style character sequence equal to string

should not be deallocated; it is owned by string object

C. Source Code

17.5 std::string assignment and swapping (Learn C++)

A. string assignment

1. assignment operator ‘=

string& operator= (const string& str)

string& operator= (const char* str)

string& operator= (char c)

2. assignment method ‘assign()

string& assign (const string& str)

string& assign (const char * str)

string& assign (const string &str, size_t subpos, size_t sublen)

string& assign (size_t n, char c)

B. swapping

void string::swap (string& str)

void swap (string& str1, string& str2)

C. Source Code

17.6 string appending (Learn C++)

A. appending operator ‘+=

string& operator+= (const string& str)

string& operator+= (const char * s)

string& operator+= (char c)

B. appending method ‘append()

string& append (const string& str)

string& append (const string& str, size_t subpos, size_t sublen)

string& append (const char* s)

string& append (const char* s, size_t n) – not recommend to use

string& append (size_t n, char c)

C. appending method ‘push_back()

void push_back (char c)

D. Source Code

17.7 std::string inserting (Learn C++)

A. inserting method ‘insert()

string& insert (size_t pos, const string& str)

string& insert (size_t pos, const string& str, size_t subpos, size_t sublen)

string& insert (size_t pos, const char* s)

string& insert (size_t pos, const char* s, size_t n)

string& insert (size_t pos, size_t n, char c)

B. Source Code

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s