1. 程式人生 > 其它 >c++ string STl 標準模板庫(待完善)

c++ string STl 標準模板庫(待完善)

String STl

1.建構函式

表1 string類的建構函式,參考C++primeplus第六版

構 造 函 數 描 述
string(const char * s) 將string物件初始化為s指向的NBTS
string(size_type n, char c) 建立一個包含n個元素的string物件,其中每個元素都被初始化為字元c
string(const string & str) 將一個string物件初始化為string物件str(複製建構函式)
string( ) 建立一個預設的sting物件,長度為0(預設建構函式)
string(const char * s, size_type n)
將string物件初始化為s指向的NBTS的前n個字元,即使超過了NBTS結尾
template<class Iter> string(Iter begin, Iter end) 將string物件初始化為區間[begin, end)內的字元,其中begin和end的行為就像指標,用於指定位置,範圍包括begin在內,但不包括end
string(const string & str, string size_type pos = 0, size_type n = npos) 將一個string物件初始化為物件str中從位置pos開始到結尾的字元,或從位置pos開始的n個字元
string(string && str) noexcept 這是C++11新增的,它將一個string物件初始化為string物件str,並可能修改str(移動建構函式)
string(initializer_list<char> il) 這是C++11新增的,它將一個string物件初始化為初始化列表il中的字元

示例:

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    //使用char*字串構造
    string one("Lottery Winner!");
    cout << one << endl;
    //構造含有n個字元的字串
    string two(20, '$');
    cout << two << endl;
    //複製構造
    string three(one);
    cout << three << endl;
    //運算子過載
    one += " Oops!";
    cout << one << endl;
    two = "Sorry! That was ";
    //按位賦值
    three[0] = 'P';
    string four;
    four = two + three;
    cout << four << endl;
    char alls[] = "All's well that ends well";
    //使用一個char*字串的前幾位構造,可以超出源字串範圍
    string five(alls, 20);
    cout << five << "!\n";
    //使用迭代器構造
    string six(alls + 6, alls + 10);
    cout << six << ",";
    string seven(&five[6], &five[10]);
    cout << seven << "...\n";
    //使用字串中的某位的開端到某位的開端
    string eight(four, 7, 16);
    cout << eight << " in motion!" << endl;
    return 0;
}

輸出:

Lottery Winner!
$$$$$$$$$$$$$$$$$$$$
Lottery Winner!
Lottery Winner! Oops!
Sorry! That was Pottery Winner!
All's well that ends!
well,well...
That was Pottery in motion!

2.迭代器

Iterators: 描述
begin 提供正向迭代器支援
end 提供正向迭代器支援
rbegin 提供逆向迭代器支援
rend 提供逆向迭代器支援
cbegin 提供const迭代器支援
cend 提供const迭代器支援
crbegin 提供const逆向迭代器支援

示例:

#include <iostream>
#include <string>

int main()
{
    using namespace std;
    string one("Lottery Winner!");
    string::const_iterator ptr = one.cbegin();
    string two(one.begin(), one.begin() + 9);
    string three(one.end() - 9, one.end());
    string four(one.rbegin(), one.rend());
    cout << *ptr << endl;
    *one.begin() = 'l';
    cout << "one:" << one << endl;
    cout << "two: " << two << endl;
    cout << "three: " << three << endl;
    cout << "four: " << four << endl;
    return 0;
}

輸出:

L
one:lottery Winner!
two: Lottery 
three: y Winner!
four: !renniW yrettoL

3.Capacity:

Capacity: 描述
size 返回字串長度
length 返回字串長度
max_size 返回字元的最大可能個數
resize 改變字元數量
capacity 返回重新分配之前的字元容量
reserve 保留記憶體以儲存一定數量的字元
clear 改變字元數量
empty 判斷字串是否為空
shrink_to_fit Shrink to fit (public member function )

示例:


輸出


c

4.Element access:

Element access:
運算子[ ] 以下標存取單一字元
at 以下標存取單一字元
back 獲取末尾的字元
front 獲取開頭的字元

示例:


輸出


c

5.Modifiers:

Modifiers: 描述
[運算子]+= 在末尾增加字串
append 在末尾增加字串
push_back 在末尾增加單個字元
assign 給一部分賦值
insert 插入字元
erase 刪除字元
replace 替換字元
swap 交換兩個字串的內容
pop_back 刪除末尾字元

示例:


輸出


c

6.String operations:

String operations:
c_str 將內容以 C - string 形式返回
data 將內容以字元陣列形式返回
get_allocator 返回和物件相關的分配器的一個拷貝
copy 將內容複製為一個 C - string
find 正向搜尋第一次出現的某子字串或字元
rfind 反向搜尋第一次出現的某子字串或字元
find_first_of 尋找單字元在字串中的第一個下標
find_last_of 尋找單字元在字串中的最後一個下標
find_first_not_of 尋找單字元在字串中缺失後的第一個下標
find_last_not_of 尋找單字元在字串中缺失後的第一個下標
substr 返回子字串
compare 比較字串內容

示例:


輸出


c

7.Member constants

Member constants
npos Maximum value for size_t (public static member constant )

最大值:18446744073709551615

示例:


輸出


c

8.非成員函式的過載

Non-member function overloads
運算子+ 連線兩個字串
關係運算符 ==, !=, <, <=, >, >= 按照ascII碼值比較字串的每一位; 如果直到某個字串結束都相等,長的大
swap 交換兩個字串的內容
運算子>> 從流中提取字串 ,例如cin>>
運算子<< 將字串插入流中,例如cout<<
getline Get line from stream into string (function )

示例:


輸出


c

參考:

http://c.biancheng.net/view/1441.html

https://www.cplusplus.com/reference/string/string/?kw=string

c++primeplus 第16章