C++ 字符串类及其应用?
C++ 具有字符串类。这不同于传统的 C 字符串。C 字符串实际上是字符数组。在 C++ 中,字符串类有一些不同的属性。它有不同的函数,可以用来执行不同的任务。这里我们将看到字符串类的重要特性。
在第一部分中,我们将看到字符串类的构造函数如何以不同的方式工作。让我们通过示例了解一下。
示例
#include<iostream> using namespace std; int main() { string str("This is a string"); cout << "String is: " << str << endl; string str2(str); // initialization by another string str cout << "String is: " << str2 << endl; string str3(5, 'A'); //initialize by a character cout << "String is: "<< str3 << endl; string str4(str, 5, 10); //initialize using another string from index 5 to index 10 cout << "String is: " << str4 << endl; string str5(str.begin(), str.begin() + 7); //initialize using str from first to index 7 cout << "String is: " << str5 << endl; }
输出
String is: This is a string String is: This is a string String is: AAAAA String is: is a strin String is: This is
现在,让我们讨论一下字符串类的某些运算符。基本运算符是赋值运算符 (=)、连接运算符 (+)、索引运算符 ([ ])。让我们来看一下这些运算符的示例。
示例
#include<iostream> using namespace std; int main() { string str = "Hello "; string str2 = "World"; string str3, str4; str3 = str; //use assignment operator cout << "The value of str3: " << str3 << endl; str4 = str + str2; //concatenate two strings cout << "The value of str4: " << str4 << endl; cout << "Character at position 1 of str: " << str[1] << endl; }
输出
The value of str3: Hello The value of str4: Hello World Character at position 1 of str: e
最后,让我们看一些字符串函数。这些函数用于执行与字符串相关的一些重要任务。让我们在示例代码中看一看这些函数的作用
示例
#include<iostream> using namespace std; int main() { string str = "Hello"; cout << "String before clear: " << str << endl; str.clear(); //clean the string cout << "String after clear: " << str << endl; str = "This is a string:"; cout << "String length using length() and size() functions: " <<str.length() << " and " << str.size() << endl; cout << "Character at position 1 of str: " << str.at(1) << endl; cout << "First character of str: " << str.front() << endl; cout << "Last character of str: " << str.back() << endl; cout << "String to C like character array: " << str.c_str() << endl; cout << "String after appending text : " << str.append("ABCDEF") << endl; string str2 = "ANOTHER STRING"; cout << "String after appending text from str2 : " << str.append(str2,0, 5) << endl; //find function to find substring if (str.find("is") != string::npos) cout << "\"is\" is found in str at " << str.find("is") << " pos" <<endl; else cout << "substring not found in str" << endl; cout << "Substring of length 3 from index 5: " <<str.substr(5, 3) <<endl; cout << "String after erasing 4 characters from index 5: " <<str.erase(5, 4) << endl; cout << "Replace 7 characters from index 3: " <<str.replace(3, 7, "C++Programming"); }
输出
String before clear: Hello String after clear: String length using length() and size() functions: 17 and 17 Character at position 1 of str: h First character of str: T Last character of str: : String to C like character array: This is a string: String after appending text : This is a string:ABCDEF String after appending text from str2 : This is a string:ABCDEFANOTH "is" is found in str at 2 pos Substring of length 3 from index 5: is String after erasing 4 characters from index 5: This string:ABCDEFANOTH Replace 7 characters from index 3: ThiC++ Programmingng:ABCDEFANOTH
广告