欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++之STL--string的使用

 更新時間:2025年06月03日 10:48:16   作者:zzh_zao  
這篇文章主要介紹了C++之STL--string的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

深入探索 C++ STL 中的 std::string

在 C++ 編程中,字符串處理是一個常見的任務(wù),而 C++ 標(biāo)準(zhǔn)模板庫(STL)中的 std::string 類為我們提供了強(qiáng)大的功能來簡化這一過程。

一、std::string 的基本概念

std::string 是 C++ STL 中的一個類,用于表示和操作字符串。它屬于 <string> 頭文件,是基于模板的 std::basic_string 類的一個特化版本,專門用于處理字符類型為 char 的字符串。與傳統(tǒng)的 C 風(fēng)格字符串(以空字符結(jié)束的字符數(shù)組)相比,std::string 提供了更高的安全性和更豐富的功能。

1. 內(nèi)存管理

std::string 內(nèi)部會動態(tài)管理內(nèi)存,這意味著你不需要手動分配和釋放內(nèi)存。

當(dāng)你對字符串進(jìn)行修改時,std::string 會自動調(diào)整內(nèi)存大小以適應(yīng)新的內(nèi)容。

  • 例如:
std::string str = "Hello";
str += ", World!"; // 內(nèi)部自動調(diào)整內(nèi)存大小

2. 安全性

std::string 不會像 C 風(fēng)格字符串那樣容易出現(xiàn)越界訪問或內(nèi)存泄漏的問題。

它提供了邊界檢查和異常處理機(jī)制,確保字符串操作的安全性。

二、std::string 的構(gòu)造與初始化

std::string 提供了多種構(gòu)造方式,可以根據(jù)不同的需求選擇合適的構(gòu)造函數(shù)。

1. 默認(rèn)構(gòu)造

std::string str; // 創(chuàng)建一個空字符串

2. 從 C 風(fēng)格字符串構(gòu)造

const char* c_str = "Hello";
std::string str(c_str); // 使用 C 風(fēng)格字符串初始化

3. 從字符串的一部分構(gòu)造

std::string str1 = "Hello World";
std::string str2(str1, 6, 5); // 從 str1 的第 6 個字符開始,取 5 個字符
// str2 的內(nèi)容為 "World"

4. 使用重復(fù)字符構(gòu)造

std::string str(5, 'a'); // 創(chuàng)建一個包含 5 個 'a' 的字符串
// str 的內(nèi)容為 "aaaaa"

三、std::string 的常用操作

1. 字符串拼接

std::string 提供了多種方式來拼接字符串,包括使用 + 運算符和 += 運算符。

std::string str1 = "Hello";
std::string str2 = "World";
std::string str3 = str1 + ", " + str2 + "!"; // 使用 + 拼接
str1 += " "; // 使用 += 拼接
str1 += str2;
// str1 的內(nèi)容為 "Hello World"

2. 字符串比較

std::string 支持使用比較運算符(如 ==、!=<、> 等)來比較字符串內(nèi)容。

std::string str1 = "apple";
std::string str2 = "banana";
if (str1 < str2) {
    std::cout << "apple is less than banana" << std::endl;
}

3. 字符串查找

std::string 提供了 find 方法,用于查找子字符串的位置。

std::string str = "Hello World";
size_t pos = str.find("World");
if (pos != std::string::npos) {
    std::cout << "Found 'World' at position " << pos << std::endl;
}

4. 字符串替換

std::stringreplace 方法可以用來替換字符串中的部分內(nèi)容。

std::string str = "Hello World";
str.replace(6, 5, "Universe");
// str 的內(nèi)容變?yōu)?"Hello Universe"

5. 字符串分割

雖然 std::string 沒有直接提供分割字符串的方法,但可以通過循環(huán)和 find 方法實現(xiàn)。

std::string str = "apple,banana,orange";
std::string delimiter = ",";
size_t pos = 0;
std::string token;
while ((pos = str.find(delimiter)) != std::string::npos) {
    token = str.substr(0, pos);
    std::cout << token << std::endl;
    str.erase(0, pos + delimiter.length());
}
std::cout << str << std::endl; // 輸出最后一個子字符串

6. 字符串大小和容量

std::string 提供了 sizecapacity 方法來獲取字符串的長度和當(dāng)前分配的內(nèi)存容量。

std::string str = "Hello";
std::cout << "Size: " << str.size() << std::endl; // 輸出 5
std::cout << "Capacity: " << str.capacity() << std::endl; // 輸出實際分配的容量

7. 字符訪問

可以通過下標(biāo)運算符 []at 方法訪問字符串中的字符。

std::string str = "Hello";
char c = str[0]; // 使用下標(biāo)訪問
char d = str.at(1); // 使用 at 方法訪問

四.string類對象的訪問及遍歷操作

std::string的容量相關(guān)概念

在深入了解容量操作之前,我們需要明確幾個與std::string容量相關(guān)的重要概念:

1. size(或length)

size函數(shù)返回字符串中實際存儲的字符數(shù)量,不包括末尾的空字符'\0'。length函數(shù)與size功能完全相同,只是名字不同。

std::string str = "Hello, World!";
std::cout << "Size: " << str.size() << std::endl; // 輸出:Size: 13

2. capacity

capacity函數(shù)返回字符串當(dāng)前分配的內(nèi)存容量,單位是字符數(shù)。

capacity通常大于或等于size,因為std::string會預(yù)留一些額外的內(nèi)存,以便在字符串?dāng)U展時減少內(nèi)存分配的次數(shù)。

std::string str = "Hello, World!";
std::cout << "Capacity: " << str.capacity() << std::endl; // 輸出:Capacity: 15(具體值可能因?qū)崿F(xiàn)而異)

3. max_size

max_size函數(shù)返回std::string能夠存儲的最大字符數(shù)。

這個值通常取決于系統(tǒng)的內(nèi)存限制和std::string的實現(xiàn)。

std::string str;
std::cout << "Max size: " << str.max_size() << std::endl; // 輸出:Max size: 18446744073709551615(64位系統(tǒng))

reserve:預(yù)留內(nèi)存

reserve函數(shù)用于顯式地為std::string預(yù)留一定量的內(nèi)存。這可以減少在字符串頻繁擴(kuò)展時的內(nèi)存分配次數(shù),從而提高性能。

使用場景

當(dāng)你知道字符串將要存儲大量字符時,可以使用reserve預(yù)先分配足夠的內(nèi)存。

例如,當(dāng)你從文件中讀取大量文本并拼接到字符串中時,reserve可以顯著提高效率。

示例代碼

std::string str;
str.reserve(1000); // 預(yù)留1000個字符的內(nèi)存
for (int i = 0; i < 1000; ++i) {
    str += 'a';
}
std::cout << "Size: " << str.size() << std::endl; // 輸出:Size: 1000
std::cout << "Capacity: " << str.capacity() << std::endl; // 輸出:Capacity: 1000

注意事項

  • reserve不會改變字符串的內(nèi)容,只是改變其容量。
  • 如果reserve的參數(shù)小于當(dāng)前capacitystd::string的容量不會減少。
  • reserve是一個非強(qiáng)制性操作,具體行為可能因標(biāo)準(zhǔn)庫的實現(xiàn)而異。

shrink_to_fit:收縮內(nèi)存

shrink_to_fit函數(shù)用于收縮std::string的容量,使其盡可能接近實際的size。

這可以減少不必要的內(nèi)存占用,但需要注意的是,shrink_to_fit是一個非強(qiáng)制性操作,具體行為可能因標(biāo)準(zhǔn)庫的實現(xiàn)而異。

使用場景

當(dāng)你完成了一個字符串的頻繁修改操作后,可以使用shrink_to_fit來釋放多余的內(nèi)存。

例如,在字符串拼接完成后,調(diào)用shrink_to_fit可以優(yōu)化內(nèi)存使用。

示例代碼

std::string str = "Hello";
str += "World";
str.shrink_to_fit();
std::cout << "Size: " << str.size() << std::endl; // 輸出:Size: 10
std::cout << "Capacity: " << str.capacity() << std::endl; // 輸出:Capacity: 10(具體值可能因?qū)崿F(xiàn)而異)

注意事項

  • shrink_to_fit不會改變字符串的內(nèi)容,只是嘗試減少其容量。
  • shrink_to_fit是一個非強(qiáng)制性操作,可能不會立即生效。

resize:調(diào)整字符串大小

resize函數(shù)用于調(diào)整字符串的大小。如果新的大小大于當(dāng)前size,則會用指定的字符填充新分配的部分;如果新的大小小于當(dāng)前size,則會截斷字符串。

示例代碼

std::string str = "Hello, World!";
str.resize(5); // 截斷字符串
std::cout << "Resized string: " << str << std::endl; // 輸出:Hello

str.resize(10, 'x'); // 擴(kuò)展字符串并用'x'填充
std::cout << "Resized string: " << str << std::endl; // 輸出:Helloxxxxx

注意事項

  • resize會改變字符串的內(nèi)容,而reserveshrink_to_fit不會。
  • resize的第二個參數(shù)是可選的,默認(rèn)填充字符為空字符'\0'

容量操作的性能優(yōu)化

合理使用std::string的容量操作可以顯著提高程序的性能。

以下是一些優(yōu)化建議:

1. 預(yù)留足夠的內(nèi)存

如果你知道字符串將要存儲大量字符,使用reserve預(yù)先分配足夠的內(nèi)存可以減少內(nèi)存分配的次數(shù)。

2. 及時收縮內(nèi)存

在完成字符串的頻繁修改操作后,使用shrink_to_fit可以釋放多余的內(nèi)存,優(yōu)化內(nèi)存使用。

3. 避免不必要的拷貝

std::string的拷貝構(gòu)造和賦值操作可能會導(dǎo)致不必要的內(nèi)存分配和拷貝。如果可能,盡量使用引用或std::move來避免拷貝。

五.string類對象的修改操作

std::string的修改操作概述

std::string提供了多種方法來修改字符串的內(nèi)容,包括插入、刪除、替換、直接修改字符等。

這些操作使得字符串的編輯變得非常靈活,同時也避免了C語言中直接操作字符數(shù)組時可能出現(xiàn)的內(nèi)存錯誤。

直接修改字符

std::string支持通過下標(biāo)操作符[]at函數(shù)直接修改字符串中的字符。

這兩種方式的主要區(qū)別在于,[]操作符不進(jìn)行邊界檢查,而at函數(shù)會進(jìn)行邊界檢查并拋出異常。

示例代碼

std::string str = "Hello, World!";
str[0] = 'h'; // 將第一個字符修改為小寫的'h'
str.at(7) = 'w'; // 將第8個字符(索引為7)修改為小寫的'w'
std::cout << str << std::endl; // 輸出:hello, world!

注意事項

  • 使用[]操作符時,如果索引超出字符串范圍,行為是未定義的。
  • 使用at函數(shù)時,如果索引超出范圍,會拋出std::out_of_range異常。

插入操作

std::string提供了多種插入操作,允許你在字符串的任意位置插入字符、字符串或字符數(shù)組。

1. 插入單個字符

std::string str = "Hello, World!";
str.insert(5, "X"); // 在索引5的位置插入字符'X'
std::cout << str << std::endl; // 輸出:HelloX, World!

2. 插入字符串

std::string str = "Hello, World!";
str.insert(7, "C++"); // 在索引7的位置插入字符串"C++"
std::cout << str << std::endl; // 輸出:Hello, C++World!

3. 插入字符數(shù)組

std::string str = "Hello, World!";
char arr[] = "Python";
str.insert(7, arr); // 在索引7的位置插入字符數(shù)組"Python"
std::cout << str << std::endl; // 輸出:Hello, PythonWorld!

注意事項

  • 插入操作會改變字符串的sizecapacity。
  • 如果插入位置超出字符串范圍,行為是未定義的。

刪除操作

std::string提供了erase函數(shù),用于刪除字符串中的一部分內(nèi)容。

示例代碼

std::string str = "Hello, World!";
str.erase(7, 5); // 從索引7開始刪除5個字符
std::cout << str << std::endl; // 輸出:Hello, !

參數(shù)說明

  • 第一個參數(shù)是刪除的起始位置(索引)。
  • 第二個參數(shù)是刪除的字符數(shù)。

注意事項

  • 如果刪除的起始位置超出字符串范圍,行為是未定義的。
  • 刪除操作會改變字符串的size,但不會改變capacity。

替換操作

std::string提供了replace函數(shù),用于替換字符串中的一部分內(nèi)容。

示例代碼

std::string str = "Hello, World!";
str.replace(7, 5, "C++"); // 從索引7開始,替換5個字符為"C++"
std::cout << str << std::endl; // 輸出:Hello, C++!

參數(shù)說明

  • 第一個參數(shù)是替換的起始位置(索引)。
  • 第二個參數(shù)是替換的字符數(shù)。
  • 第三個參數(shù)是新的內(nèi)容。

注意事項

  • 替換操作會改變字符串的size,但不會改變capacity。
  • 如果替換的起始位置超出字符串范圍,行為是未定義的。

追加操作

std::string提供了多種追加操作,允許你在字符串的末尾添加字符、字符串或字符數(shù)組。

1. 追加單個字符

std::string str = "Hello";
str.push_back(' '); // 追加一個空格
str += 'W'; // 追加字符'W'
std::cout << str << std::endl; // 輸出:Hello W

2. 追加字符串

std::string str = "Hello ";
str += "World"; // 追加字符串"World"
std::cout << str << std::endl; // 輸出:Hello World

3. 追加字符數(shù)組

std::string str = "Hello ";
char arr[] = "C++";
str.append(arr); // 追加字符數(shù)組"C++"
std::cout << str << std::endl; // 輸出:Hello C++

注意事項

  • 追加操作會改變字符串的size,但不會改變capacity。
  • 如果capacity不足,std::string會自動分配更大的內(nèi)存。

清空字符串

std::string提供了clear函數(shù),用于清空字符串的內(nèi)容,但不會釋放內(nèi)存。

示例代碼

std::string str = "Hello, World!";
str.clear();
std::cout << "Size: " << str.size() << std::endl; // 輸出:Size: 0
std::cout << "Capacity: " << str.capacity() << std::endl; // 輸出:Capacity: 15(具體值可能因?qū)崿F(xiàn)而異)

注意事項

  • clear函數(shù)只會清空字符串的內(nèi)容,不會釋放內(nèi)存。
  • 如果需要釋放內(nèi)存,可以結(jié)合shrink_to_fit使用。

使用std::stringstream進(jìn)行復(fù)雜修改

對于復(fù)雜的字符串修改操作,可以使用std::stringstream。

std::stringstream是一個非常靈活的工具,可以方便地進(jìn)行字符串的拼接、格式化和提取。

示例代碼

#include <sstream>

std::string str = "Hello, World!";
std::stringstream ss(str);
std::string word;
ss >> word; // 提取第一個單詞
ss << "C++"; // 替換為"C++"
ss >> word; // 提取第二個單詞
ss << "Programming"; // 替換為"Programming"
str = ss.str(); // 將修改后的內(nèi)容賦值回str
std::cout << str << std::endl; // 輸出:Hello, C++Programming

六.string類非成員函數(shù)

什么是std::string的非成員函數(shù)?

std::string的非成員函數(shù)是指那些不屬于std::string類的成員函數(shù),但專門用于操作std::string對象的函數(shù)。

這些函數(shù)通常定義在<string>頭文件中,或者通過標(biāo)準(zhǔn)庫的其他部分提供。

非成員函數(shù)的優(yōu)勢在于它們可以提供更通用的操作方式,同時避免了對std::string類本身的過度擴(kuò)展。

常見的std::string非成員函數(shù)

1. std::swap

std::swap用于交換兩個std::string對象的內(nèi)容。

它是一個非成員函數(shù),可以高效地交換兩個字符串的內(nèi)容,而不需要逐個字符復(fù)制。

示例代碼

#include <iostream>
#include <string>
#include <algorithm> // 包含std::swap

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";

    std::swap(str1, str2);

    std::cout << "str1: " << str1 << std::endl; // 輸出:str1: World
    std::cout << "str2: " << str2 << std::endl; // 輸出:str2: Hello
}

2. std::to_string

std::to_string是一個非常有用的非成員函數(shù),用于將數(shù)值類型(如int、float、double等)轉(zhuǎn)換為std::string。

它提供了一種簡單且類型安全的方式來實現(xiàn)數(shù)值與字符串之間的轉(zhuǎn)換。

示例代碼

#include <iostream>
#include <string>

int main() {
    int num = 42;
    double pi = 3.14159;

    std::string numStr = std::to_string(num);
    std::string piStr = std::to_string(pi);

    std::cout << "Number as string: " << numStr << std::endl; // 輸出:Number as string: 42
    std::cout << "Pi as string: " << piStr << std::endl; // 輸出:Pi as string: 3.141590
}

3. std::stoi、std::stod、std::stof等

這些函數(shù)用于將std::string對象轉(zhuǎn)換為數(shù)值類型。

std::stoi將字符串轉(zhuǎn)換為int,std::stod將字符串轉(zhuǎn)換為double,std::stof將字符串轉(zhuǎn)換為float等。

這些函數(shù)提供了一種簡單且安全的方式來解析字符串中的數(shù)值。

示例代碼

#include <iostream>
#include <string>

int main() {
    std::string numStr = "42";
    std::string piStr = "3.14159";

    int num = std::stoi(numStr);
    double pi = std::stod(piStr);

    std::cout << "Number from string: " << num << std::endl; // 輸出:Number from string: 42
    std::cout << "Pi from string: " << pi << std::endl; // 輸出:Pi from string: 3.14159
}

4. std::getline

std::getline是一個非成員函數(shù),用于從輸入流(如std::cinstd::ifstream)中讀取一行內(nèi)容并存儲到std::string對象中。它支持按分隔符讀取,例如按逗號、空格等分隔。

示例代碼

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string line = "Hello, World!";
    std::istringstream ss(line);
    std::string word;

    while (std::getline(ss, word, ',')) {
        std::cout << word << std::endl; // 輸出:Hello 和 World!
    }
}

5. std::operator<< 和 std::operator>>

這些操作符重載允許你直接使用std::cinstd::cout來輸入和輸出std::string對象。

它們是C++標(biāo)準(zhǔn)庫提供的非成員函數(shù),使得字符串的輸入輸出變得非常方便。

示例代碼

#include <iostream>
#include <string>

int main() {
    std::string str;
    std::cout << "Enter a string: ";
    std::cin >> str;

    std::cout << "You entered: " << str << std::endl;
}

6. std::operator+ 和 std::operator+=

雖然這些操作符是std::string的成員函數(shù),但它們也可以作為非成員函數(shù)使用。

std::operator+用于拼接兩個字符串,而std::operator+=用于將一個字符串追加到另一個字符串的末尾。

這些操作符使得字符串拼接變得非常直觀。

示例代碼

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";

    std::string result = str1 + " " + str2; // 使用std::operator+
    str1 += " "; // 使用std::operator+=
    str1 += str2;

    std::cout << "Result: " << result << std::endl; // 輸出:Result: Hello World
    std::cout << "Modified str1: " << str1 << std::endl; // 輸出:Modified str1: Hello World
}

7. std::operator==、std::operator!=、std::operator<等

這些操作符重載允許你直接比較兩個std::string對象。它們按照字典序進(jìn)行比較,使得字符串的比較變得非常方便。

示例代碼

#include <iostream>
#include <string>

int main() {
    std::string str1 = "Hello";
    std::string str2 = "World";

    if (str1 == str2) {
        std::cout << "Strings are equal." << std::endl;
    } else {
        std::cout << "Strings are not equal." << std::endl; // 輸出:Strings are not equal.
    }

    if (str1 < str2) {
        std::cout << "str1 is less than str2." << std::endl; // 輸出:str1 is less than str2.
    }
}

總結(jié)

std::string 是 C++ STL 中一個非常強(qiáng)大且靈活的工具,它提供了豐富的功能來處理字符串,同時避免了許多傳統(tǒng)字符串操作的常見問題。

通過掌握 std::string 的構(gòu)造、操作、性能優(yōu)化以及高級用法,你可以更高效地編寫字符串處理相關(guān)的代碼。

在實際開發(fā)中,合理使用 std::string 能夠大大提高代碼的可讀性和安全性。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C++使用GDAL庫實現(xiàn)Tiff文件的讀取

    C++使用GDAL庫實現(xiàn)Tiff文件的讀取

    這篇文章主要為大家詳細(xì)介紹了C++使用GDAL庫實現(xiàn)Tiff文件的讀取的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-03-03
  • C++ TinyXML解析案例詳解

    C++ TinyXML解析案例詳解

    這篇文章主要介紹了C++ TinyXML解析案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C語言所有經(jīng)典排序方法的實現(xiàn)代碼

    C語言所有經(jīng)典排序方法的實現(xiàn)代碼

    這篇文章給大家分享C語言所有經(jīng)典排序方法,文章給大家提供完整的實例代碼幫助大家快速學(xué)習(xí)掌握C語言排序方法,感興趣的朋友一起看看吧
    2021-06-06
  • C++實現(xiàn)遞歸函數(shù)的方法

    C++實現(xiàn)遞歸函數(shù)的方法

    在本篇內(nèi)容里小編給大家分享了關(guān)于C++實現(xiàn)遞歸函數(shù)的教學(xué)步驟,需要的朋友跟著參考下。
    2018-12-12
  • Qt使用QCustomPlot的實現(xiàn)示例

    Qt使用QCustomPlot的實現(xiàn)示例

    QCustomPlot是一個基于Qt C++的圖形庫,用于繪制和數(shù)據(jù)可視化,并為實時可視化應(yīng)用程序提供高性能服務(wù),本文主要介紹了Qt使用QCustomPlot的實現(xiàn)示例,感興趣的可以了解一下
    2024-01-01
  • 基于Matlab實現(xiàn)抖音小游戲蘋果蛇

    基于Matlab實現(xiàn)抖音小游戲蘋果蛇

    最近抖音上蘋果蛇小游戲大火,為了證明MATLAB無所不能,咋能不跟風(fēng)做一個?文中詳細(xì)講解了游戲的實現(xiàn)步驟,感興趣的小伙伴可以嘗試一下
    2022-06-06
  • C語言字符串替換:字符,字符串,字符數(shù)組詳解

    C語言字符串替換:字符,字符串,字符數(shù)組詳解

    這篇文章主要介紹了C++字符串替換的字符,字符串,字符數(shù)組,需要考慮的情況比較全面,有不錯的借鑒價值,需要的朋友可以參考下
    2021-09-09
  • C++命名空間使用詳細(xì)介紹

    C++命名空間使用詳細(xì)介紹

    考慮一種情況,當(dāng)我們有兩個同名的人,Zara,在同一個班里。當(dāng)我們需要對它們進(jìn)行區(qū)分我們必須使用一些額外的信息和它們的名字,比如它們生活在不同的區(qū)域或者興趣愛好什么的,在C++程序中也會遇到同樣的情況,所以命名空間就此產(chǎn)生
    2022-09-09
  • C++11中異常處理機(jī)制詳解

    C++11中異常處理機(jī)制詳解

    傳統(tǒng)的C語言處理異常的方式有兩種:終止程序和返回錯誤碼。在實際中的C語言程序基本都是通過返回錯誤碼的方式來處理錯誤的,部分情況下使用終止程序來處理比較嚴(yán)重的錯誤。本文將通過示例和大家聊聊C++11中異常處理機(jī)制,需要的可以參考一下
    2022-09-09
  • 有關(guān)C++頭文件的包含順序研究

    有關(guān)C++頭文件的包含順序研究

    下面小編就為大家?guī)硪黄嘘P(guān)C++頭文件的包含順序研究。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論