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
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP