C STL感知 string容器

2021-10-08 12:43:31 字數 4906 閱讀 5731

在c++中經常如下定義字串

string str;
string本質是c++中的乙個類。char *是乙個指標,string類內部封裝了char *

string類的建構函式:

string()

;//建立控的字串

sring

(const

char

*s);

//使用s初始化字串

string

(const string &str)

;//拷貝建構函式,使用另乙個字串初始化初始化字串

string

(int n,

char c)

;//使用n個字元c初始化字串

string str1

("hello world");

string str2

(str1)

;//使用拷貝建構函式初始化str2

string str3

(520

,'h');

//使用520個h字元初始化str3

string賦值操作

對應建構函式string類的賦值操作就有很多種

char

*s =

"hello world"

;string str

(s);

cout << str << endl;

string str1;

str1.

assign

(str)

;//將字串str的值賦值給str1

cout << str1 << endl;

string str2;

str2.

assign

(s);

//將指標s存放的字串賦值給str2

string str3;

str3.

assign

(s,5);

//字串s的前5個字元賦值給str3

cout << str3 << endl;

string str4;

str4 =

'h';

//將字元h賦值給str4

cout << str4 << endl;

string str5;

str5.

assign

('h'

,520);

//將520個字元h賦值給str5

string str6 =

"i love u."

;//直接賦值

賦值操作很多很建構函式初始化字串差不多。

string字串拼接

char

*s =

"hello"

;string str1 = s;

str1 +

=",linux"

;//在字串後追加乙個字串

cout << str1 << endl;

string str2 =

"i love "

;str2 +

='u'

;//在字串後面追加乙個字元

cout << str2 << endl;

char

*ss =

"love u"

;string str3 =

"i "

;str3 +

= ss;

//cout << str3 << endl;

//使用string類的方法拼接字串

string str4 =

"aaa "

;str4.

(str3)

;//使用string類的方法在str4後面拼接str3

cout << str4 << endl;

string str5 =

"bbb "

;str5.

(s,4);

//將字串的s的前5個字元拼接到str5

cout << str5 << endl;

string str6;

str6.

(str1,0,

4);//將字串str1的第0個位置開始的5個字元拼接到str6

cout << str6 << endl;

string查詢、替換

string類中關於字串查詢替換的方法:

find()方法是從左往右查詢,rfind()方法是從右往左查詢

//字串查詢

intfind

(const string &str,

int pos =0)

;//從pos(預設為0)位置查詢,str字串第一次出現的位置

intfind

(const

char

*s,int pos =0)

;int

find

(const

char

* s,

int pos,

int n)

;//從pos位置開始查詢s字串前n個字元第一次出現位置

intfind

(const

char c,

int pos =0)

;int

rfind

(const string& str,

int pos = npos)

;//從pos位置開始查詢str最後一次出現的位置

intrfind

(const

char

* s,

int pos = npos)

;int

rfind

(const

char

* s,

int pos,

int n)

;//從pos位置查詢s的前n個字元最後一次位置

intrfind

(const

char c,

int pos =0)

;//字串替換

string &

replace

(int pos,

int n,

const string& str)

;//從pos開始n個字元替換為字串str

string &

replace

(int pos,

int n,

const

char

* s)

;

void

find_replace

(void

)else

pos = str.

rfind

("moon");

if(pos <0)

else

}

string字串比較

一般用來比較字串是否一致,字串相等則返回0。比較的是字串的ascii值

int

compare

(const string &s)

;//與字串s比較

intcompare

(const

char

*s);

//與字串s比較

void string_compare(void)

else if (retval > 0)

else

}

string字元訪問
char str[n]

;//通過類似陣列的方式訪問字元

char&at

(int n)

;//通過at方法訪問字元

void string_get_set(void)

cout << "\r\n";

for (int index = 0; index < str.size(); index++)

cout << endl;

str[0] = 'u'; //修改

str.at(5) = 'b';

for (int x = 0; x < str.size(); x++)

}

string插入和刪除
string&

insert

(int pos,

const

char

* s)

;//從pos位置開始插入字串s

string&

insert

(int pos,

const string& str)

;

string&

insert

(int pos,

int n,

char c)

;//在指定位置pos開始插入n個字元c

string&

erase

(int pos,

int n = npos)

;//從pos位置開始刪除n個字元

void insert_erase(void)

string子字串
string substr

(int pos =0,

int n = npos)

;//從pos位置開始返回n個字元組成的字串

void string_substr(void)

C STL感知 vector容器

vector容器非常類似陣列,也稱單端陣列 vector容器的迭代器支援隨機訪問與陣列不同 陣列是在靜態空間分配,vector可以動態擴充套件動態擴充套件原理 重新分配空間,將原空間資料拷貝到新空間,再釋放原空間建立vector容器void get vector void show vector v...

C STL操作,string容器

include include 動態陣列 可變陣列 include 演算法標頭檔案 using namespace std void printvector int v stl基本語法 void text1 容器也可以存放自定義資料型別 class person int age int id voi...

C STL篇(三)容器之string容器

3.1string 容器基本概念 c風格的字串 以空字元結尾的字元陣列 太過於複雜,難以掌握,不適合大程式的開發,c 標準庫定義了一種string類,定義在標頭檔案中 string和c的風格對比 a char是乙個指標,而string是乙個類 string封裝了char,管理字串,是char型的乙個...