Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

C++ String Class และการประยุกต์ใช้


C++ มี String class ซึ่งแตกต่างจาก C strings แบบดั้งเดิม C string จริงๆ แล้วคือ character array ใน C++ string class มีคุณสมบัติที่ต่างออกไป มีฟังก์ชันต่างๆ ที่ใช้ทำงานต่างๆ ได้ ที่นี่เราจะดูคุณสมบัติสำคัญของ String class

ในส่วนแรก เราจะดูว่า constructors ของ string class ทำงานอย่างไรแบบต่างๆ มาดูตัวอย่าง

ตัวอย่าง

#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

ตอนนี้มาดูเกี่ยวกับโอเปอเรเตอร์บางตัวของ String class โอเปอเรเตอร์พื้นฐานคือ Assignment operator (=), Concatenate operator (+), Index operator ([ ]) มาดูตัวอย่าง

ตัวอย่าง

#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