-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodern string functions in cpp
More file actions
146 lines (114 loc) · 8.57 KB
/
Copy pathmodern string functions in cpp
File metadata and controls
146 lines (114 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
In modern C++, the std::string class provides a wide range of string functions that are safer, more flexible, and easier to use than traditional C-style string functions. The std::string class is part of the C++ Standard Library (<string> header) and offers various functions to manipulate and manage strings efficiently.
Key Modern String Functions in C++
Below is a list of commonly used modern string functions in C++ with brief descriptions:
1. String Initialization and Assignment:
○ std::string s = "Hello"; — Initializes a string with a literal.
○ s = "World"; — Assigns a new value to the string.
2. String Input and Output:
○ getline(cin, s); — Reads a full line of input from the standard input (cin) into the string s.
○ cout << s; — Prints the string s to the standard output.
3. String Length and Capacity:
○ s.size() or s.length() — Returns the number of characters in the string.
○ s.capacity() — Returns the size of the allocated storage for the string.
○ s.empty() — Returns true if the string is empty; otherwise, false.
4. String Concatenation:
○ s1 + s2 — Concatenates two strings and returns a new string.
○ s += " more text"; — Appends a string literal or another string to the end of s.
5. Accessing Characters:
○ s[index] — Accesses the character at the given index (0-based indexing).
○ s.at(index) — Similar to operator[], but performs bounds checking and throws an out_of_range exception if the index is invalid.
○ s.front() — Returns a reference to the first character.
○ s.back() — Returns a reference to the last character.
6. String Modification:
○ s.insert(pos, str) — Inserts the string str at the given position pos.
○ s.erase(pos, len) — Erases len characters from position pos.
○ s.replace(pos, len, str) — Replaces len characters starting from pos with the string str.
○ s.clear() — Clears the contents of the string, making it an empty string.
7. Substring Extraction:
○ s.substr(pos, len) — Returns a substring starting at position pos with a length of len.
8. Searching in Strings:
○ s.find(str) — Searches for the first occurrence of the substring str in s and returns its position. Returns std::string::npos if not found.
○ s.rfind(str) — Searches for the last occurrence of the substring str in s.
○ s.find_first_of(chars) — Returns the first occurrence of any character from chars in s.
○ s.find_last_of(chars) — Returns the last occurrence of any character from chars in s.
9. String Comparison:
○ s1.compare(s2) — Compares two strings lexicographically. Returns 0 if they are equal,
a negative value if s1 is less than s2, and a positive value if s1 is greater than s2.
10. String Conversion:
○ std::to_string(intValue) — Converts an integer value to a string.
○ std::stoi(str) — Converts a string to an integer.
○ std::stof(str) — Converts a string to a float.
○ Similar functions include std::stol, std::stod, std::stoll, etc., for different data types.
11. Removing Whitespace:
○ s.erase(remove_if(s.begin(), s.end(), ::isspace), s.end()); — Removes all whitespace characters from a string using the erase and remove_if algorithms.
12. Iterators:
○ s.begin() and s.end() — Returns iterators to the beginning and end of the string, respectively.
○ s.rbegin() and s.rend() — Returns reverse iterators to the beginning and end of the string.
13. String Conversion to C-Style Strings:
○ s.c_str() — Returns a pointer to a null-terminated C-style string (const char*) representation of the std::string object.
List of all Modern String Functions in C++
1. Constructors:
○ std::string s; — Default constructor, creates an empty string.
○ std::string s("Hello"); — Creates a string initialized with a C-style string.
○ std::string s(n, 'x'); — Creates a string with n copies of character 'x'.
2. Capacity Functions:
○ s.size() or s.length() — Returns the number of characters in the string.
○ s.max_size() — Returns the maximum number of characters the string can hold.
○ s.capacity() — Returns the size of the allocated storage for the string.
○ s.resize(n) — Resizes the string to contain n characters.
○ s.reserve(n) — Requests a change in capacity to at least n characters.
○ s.clear() — Clears the contents of the string, making it an empty string.
○ s.empty() — Returns true if the string is empty; otherwise, false.
○ s.shrink_to_fit() — Reduces the capacity to fit the size of the string.
3. Element Access Functions:
○ s[i] — Accesses the character at index i (0-based indexing).
○ s.at(i) — Accesses the character at index i with bounds checking.
○ s.front() — Returns a reference to the first character in the string.
○ s.back() — Returns a reference to the last character in the string.
○ s.data() — Returns a pointer to the internal character array (non-const version in C++17).
○ s.c_str() — Returns a pointer to a null-terminated C-style string (const char*).
4. Modifiers:
○ s += str; — Appends a string or character to the end of s.
○ s.append(str) — Appends the string str to the end of the string.
○ s.push_back('c') — Appends a character 'c' to the end of the string.
○ s.insert(pos, str) — Inserts the string str at the specified position pos.
○ s.erase(pos, len) — Erases len characters starting from position pos.
○ s.replace(pos, len, str) — Replaces len characters starting from pos with the string str.
○ s.pop_back() — Removes the last character from the string.
○ s.swap(str2) — Exchanges the contents of the string with str2.
5. String Operations:
○ s.copy(buffer, len, pos) — Copies len characters from the string starting at pos into buffer.
○ s.substr(pos, len) — Returns a substring starting at position pos with a length of len.
○ s.find(str, pos) — Finds the first occurrence of the substring str starting from pos.
○ s.rfind(str, pos) — Finds the last occurrence of the substring str starting from pos.
○ s.find_first_of(chars, pos) — Finds the first occurrence of any character from chars starting from pos.
○ s.find_last_of(chars, pos) — Finds the last occurrence of any character from chars starting from pos.
○ s.find_first_not_of(chars, pos) — Finds the first character not in chars starting from pos.
○ s.find_last_not_of(chars, pos) — Finds the last character not in chars starting from pos.
6. String Comparison:
○ s == str2 — Checks if two strings are equal.
○ s != str2 — Checks if two strings are not equal.
○ s < str2, s > str2, s <= str2, s >= str2 — Lexicographical comparisons between strings.
○ s.compare(str2) — Compares the string s with str2 lexicographically and returns:
§ 0 if they are equal,
§ A negative value if s is less than str2,
§ A positive value if s is greater than str2.
7. Conversion Functions:
○ std::to_string(val) — Converts a number (int, float, double, etc.) to a std::string.
○ std::stoi(str) — Converts a string to an integer (int).
○ std::stol(str) — Converts a string to a long integer (long).
○ std::stoll(str) — Converts a string to a long long integer (long long).
○ std::stof(str) — Converts a string to a float (float).
○ std::stod(str) — Converts a string to a double (double).
○ std::stold(str) — Converts a string to a long double (long double).
8. Iterators:
○ s.begin() and s.end() — Returns iterators to the beginning and end of the string, respectively.
○ s.rbegin() and s.rend() — Returns reverse iterators to the beginning and end of the string.
○ s.cbegin(), s.cend(), s.crbegin(), s.crend() — Constant iterators (do not allow modification of the string).
Key Benefits of Modern String Functions in C++
• Type Safety: The std::string class provides strong type safety, avoiding many common pitfalls of C-style strings, such as buffer overflows.
• Memory Management: std::string automatically manages memory allocation and deallocation.
• Ease of Use: Functions provided by std::string are easier to use and more intuitive than their C counterparts.
• Exception Handling: Many std::string functions provide exception handling (e.g., at() throws out_of_range exceptions).
Conclusion
Modern C++ offers a rich set of string functions in the std::string class that provides safety, convenience, and flexibility for handling strings in programs. These functions are part of the C++ Standard Library and are widely used for string manipulation and processing.