Common Methods of the C++ String Class

In C++ programming, the string class is the core tool for handling strings, providing a rich set of methods to simplify string operations. Compared to C-style character arrays, the string class is safer, more convenient, and powerful, avoiding common memory management issues and boundary errors associated with C-style string processing.

1. Construction and Initialization (5 Types)

1.1 Default Constructor: string()

Creates an empty string.

Example: string str; (str is empty, str.empty() returns true)

1.2 C-String Initialization: string(const char* s)

Initializes with a C-style string.

Example: string str("hello"); (str content is “hello”)

1.3 Repeated Character Constructor: string(size_t n, char c)

Creates a string containing n repeated characters c.

Example: string str(3, 'a'); (str is “aaa”)

1.4 Copy Constructor: string(const string& str)

Copies another string object.

Example: string str1("test"); string str2(str1); (str2 is the same as str1)

1.5 Substring Constructor: string(const string& str, size_t pos, size_t len = npos)

Extracts len characters from str starting at position pos (if len is omitted, it goes to the end).

Example: string str1("abcdef"); string str2(str1, 2, 3); (str2 is “cde”)

2. Basic Properties and Operations (5 Types)

2.1 size(): Returns the length of the string (number of characters).

Example: "hello".size(); (returns 5)

2.2 empty(): Checks if the string is empty (more concise than size() == 0).

Example: string().empty(); (returns true)

2.3 swap(string& str): Swaps the contents of two strings (efficient, no extra copies).

Example: str1.swap(str2); (swaps the values of str1 and str2)

2.4 clear(): Clears the string (length becomes 0).

Example: str.clear(); (after clearing, str.empty() is true)

2.5 capacity(): Returns the maximum number of characters that can be held in the current memory (without reallocating memory).

Example: string str(10, 'a'); str.capacity(); (usually ≥ 10)

3. Assignment and Concatenation (6 Types)

3.1 operator=: Direct assignment.

Example: str2 = str1; (str2 copies the content of str1)

3.2 assign(const string& str): Assignment (same functionality as operator=, but more flexible).

Example: str.assign("new"); (str becomes “new”)

3.3 operator+: Concatenates two strings (returns a new string).

Example: string res = "a" + string("b"); (res is “ab”, note: “a” + “b” will cause an error, must convert to string("b"))

3.4 operator+=: Appends content to the current string.

Example: str += "xyz"; (appends “xyz” to the end of str)

3.5 append(const string& str): Appends a string (similar to operator+=, supports more parameters, such as append("123", 2)).

Example: str.append("123"); (equivalent to str += "123")

3.6 push_back(char c): Appends a single character (more efficient than +=).

Example: str.push_back('!'); (adds ‘!’ to the end of str)

4. Comparison Operations (3 Types)

4.1 operator==: Checks if two strings are equal.

Example: "abc" == string("abc"); (returns true)

4.2 operator!=: Checks if two strings are not equal.

Example: "abc" != "abd"; (returns true)

4.3 compare(const string& str): Fine comparison (returns an integer: 0 = equal, positive = current string is greater, negative = current string is smaller).

Example: "apple".compare("banana"); (returns a negative number, because “apple” < “banana”)

5. Searching and Positioning (6 Types)

5.1 find(const string& sub, size_t pos = 0): Searches for substring sub starting from pos, returning the index of its first occurrence (returns string::npos if not found).

Example: "abcabc".find("bc"); (returns 1)

5.2 rfind(const string& sub, size_t pos = npos): Searches for substring sub in reverse starting from pos, returning the index of its last occurrence.

Example: "abcabc".rfind("bc"); (returns 4)

5.3 find_first_of(const string& chars, size_t pos = 0): Finds the first occurrence of any character in chars.

Example: "hello".find_first_of("aeiou"); (returns 1, the position of ‘e’)

5.4 find_last_of(const string& chars, size_t pos = npos): Finds the last occurrence of any character in chars.

Example: "hello".find_last_of("aeiou"); (returns 4, the position of ‘o’)

5.5 find_first_not_of(const string& chars, size_t pos = 0): Finds the position of the first character not in chars.

Example: "xxyzz".find_first_not_of("xyz"); (returns npos, as all characters are in the set)

5.6 find_last_not_of(const string& chars, size_t pos = npos): Finds the position of the last character not in chars.

Example: "a1b2c3".find_last_not_of("abc"); (returns 5, the position of ‘3’)

6. Accessing and Substring (4 Types)

6.1 operator[]: Accesses the character at the specified index (no bounds checking, efficient).

Example: "hello"[1]; (returns ‘e’)

6.2 at(size_t pos): Accesses the character at the specified index (throws out_of_range exception on out-of-bounds, safe).

Example: "hello".at(1); (returns ‘e’)

6.3 front(): Gets the first character (modifiable).

Example: str.front() = 'A'; (changes the first character of str to ‘A’)

6.4 substr(size_t pos = 0, size_t len = npos): Extracts a substring (starting from pos, taking len characters, if len is omitted, it goes to the end).

Example: "hello world".substr(6); (returns “world”)

7. Modification and Deletion

7.1 erase(size_t pos = 0, size_t len = npos): Deletes a substring (deletes len characters starting from pos).

Example: str.erase(2, 3); (deletes 3 characters starting from index 2 in str)

7.2 pop_back(): Deletes the last character (concise and efficient).

Example: str.pop_back(); (deletes the last character of str)

7.3 Use string.insert() for insertion operations

string& insert(size_t pos, const string& str); // Inserts string str at position pos
string& insert(size_t pos, const string& str, size_t subpos, size_t sublen); // Inserts sublen characters from str starting at subpos at position pos
string& insert(size_t pos, const char * s); // Inserts string s at position pos
string& insert(size_t pos, const char * s, size_t n); // Inserts the first n characters of string s at position pos
string& insert(size_t pos, size_t n, char c); // Inserts n characters c at position pos
iterator insert(const_iterator p, size_t n, char c); // Inserts n characters c at position p and returns the iterator to the new position
iterator insert(const_iterator p, char c); // Inserts character c at position p and returns the iterator to the new position

Common Methods of the C++ String Class

Leave a Comment