一. string介紹。
string類:
string是表示字串的字串類。
該類的介面與常規容器的介面基本相同,再新增了一些專門用來操作string的常規操作。
string在底層實際是:basic_string模板類的別名,typedef basic_string
string;
不能操作多位元組或者變長字元的序列。
在使用string類時,必須包含#include標頭檔案以及using namespace std;
二.string類的常用使用介面。
1.string類的物件的常見構造:
-string() : 構造空的string類物件,即空字串
-string(const char* s) : 用c-string來構造string類物件
-string(size_t n, char c) :string類物件中包含n個字元c
-string(const string&s) : 拷貝建構函式
int
main()
2.tring類物件的容量操作:-size: 返回字串有效字元長度
-length :返回字串有效字元長度
-capacity: 返回空間總大小
-empty : 檢測字串釋放為空串,是返回true,否則 返回false
-clear : 清空有效字元
-reserve : 為字串預留空間**
-resize : 將有效字元的個數該成n個,多出的空間用字元c填充
int
main()
;//c++11 初始化列表。
cout << str1.
size()
<< endl;
// 11
cout << str1.
length()
<< endl;
// 11
cout << str1.
empty()
<< endl;
// 0
cout << str1.
capacity()
<< endl;
// 15
str1.
reserve(24
);cout << str1.
capacity()
<< endl;
// 31
str1.
resize(40
);cout << str1.
size()
<< endl;
// 40
cout << str1.
capacity()
<< endl;
// 47
cout << str1 << endl;
// ...
//從**中可以看出string類的擴容和vector的擴容很像。
//resize(size_t n) 與 resize(size_t n, char c)都是將字串中有效字元個數改變到n個,不同的是當字元個數增多時:resize(n)用0來填充多出的元素空間,
//resize(size_t n, char c)用字元c來填充多出的素空間。
//注意:resize在改變元素個數時,如果是將元素個數增多,可能會改變底層容量的大小,如果是將元素個數減少,底層空間總大小不變
str1.
clear()
; cout << str1 << endl;
cout << str1.
capacity()
<< endl;
// 47
cout << str1.
size()
<< endl;
// 0
return0;
}
注意:
size()與length()方法底層實現原理完全相同,引入size()的原因是為了與其他容器的介面保持一致,一般情況下基本都是用size()。
clear()只是將string中有效字元清空,不改變底層空間大小。resize(size_t n) 與 resize(size_t n, char c)都是將字串中有效字元個數改變到n個,不同的是當字
符個數增多時:resize(n)用0來填充多出的元素空間,resize(size_t n, char c)用字元c來填充多出的元素空間。注意:resize在改變元素個數時,如果是將元素個數增多,可能會改變底層容量的大小,如果是將元素個數減少,底層空間總大小不變。
reserve(size_t res_arg=0):為string預留空間,不改變有效元素個數,當reserve的引數小於string的底層空間總大小時,reserver不會改變容量大小。
3.string類物件的訪問及遍歷操作:
-operator :(重點) 返回pos位置的字元,const string類物件呼叫
-begin+ end : begin獲取乙個字元的迭代器 + end獲取最後乙個字元下乙個位置的迭代器rbegin + rend begin獲取乙個字元的迭代器 + end獲取最後乙個字元下乙個位置的迭代器
-範圍for : c++11支援更簡潔的範圍for的新遍歷方式
int
main()
cout << endl;
for 訪問
for(
auto
&e : str1)
cout << e;
cout << endl;
return0;
}
4.string類物件的修改操作:-push_back :在字串後尾插字元c
-operator+=: (重點) 在字串後追加字串str
-c_str:(重點) 返回c格式字串
-find + npos:(重點) 從字串pos位置開始往後找字元c,返回該字元在字串中的位置
-rfind :從字串pos位置開始往前找字元c,返回該字元在字串中的位置
-substr :在str中從pos位置開始,擷取n個字元,然後將其返回
int
main()
start +=3
;//跳過://
size_t finish = url.
find
('/'
, start)
;//找到從start開始的「:/」.
string address = url.
substr
(start, finish - start)
;//從start開始找網域名稱
cout << address << endl;
return0;
}
注意:
對string操作時,如果能夠大概預估到放多少字元,可以先通過reserve把空間預留好。
5.string類非成員函式:
-operator+ : 盡量少用,因為傳值返回,導致深拷貝效率低
-operator>> :(重點) 輸入運算子過載
-operator<< :(重點) 輸出運算子過載
-getline :(重點) 獲取一行字串
-relational operators :(重點) 大小比較
三.string的模擬實現:namespace ice
string
(const string& s)
//拷貝建構函式;
:_str
(nullptr),
_size(0
),_capacity(0
) string operator
=(string s)
//賦值語句
~string()
}///
iterator begin()
iterator end()
char
operator
(size_t i)
const
char
operator
(size_t i)
const
void
push_back
(char c)
size_t size()
size_t capacity()
void
swap
(string& s)
//交換函式
void
resize
(size_t newsize,
char c =
'\0'
)memset
(_str + _size, c, newsize - _size);}
_size = newsize;
_str[newsize]
='\0';}
void
reserve
(size_t newcapacity)
//擴容
} string&
operator+=
(char c)
const
char
*c_str()
const
// c_str 方法;
private
:char
* _str;
size_t _capacity;
size_t _size;};
}ostream& ice::
operator
<<
(ostream &out,
const string &s)
C set容器使用
stl的set是乙個二叉排序樹,也稱為集合,其在stl內部實現是紅黑樹,能夠將元素預設從小到大排序或者是字典序排序。如果宣告的元素型別不是基本資料型別而是自定義的類要給它乙個比較器,類似於sort的compare。include include include include using names...
C vector容器使用
一 vector容器四種遍歷方式 include include include using namespace std void print vector v bool mycmpare const int a,const int b 定義三個結構體 struct cvector2 struct ...
C set 容器使用
g set.cc std c 17 set 容器使用 set 內部是使用紅黑樹實現的,是一種平衡二叉樹,所以對其插入 查詢效率是非常高的,其時間複雜度是log2 n set是stl中一種標準關聯容器。它底層使用平衡的搜尋樹 紅黑樹實現,插入刪除操作時僅僅需要指標操作節點即可完成,不涉及到記憶體移動和...