C C 學習筆記 string 增刪改查

2021-08-23 12:11:32 字數 3019 閱讀 7389

功能:在字串中查詢某個字元或字串。

函式:find(str,pos),rfind(str,pos),find_first_of(str,pos),find_last_of(str,pos)。

引數:str,要查詢的字元或字串;pos,查詢的起始位置。

std::string str = "abc123abc456";

/* str=| a | b | c | 1 | 2 | 3 | a | b | c | 4 | 5 | 6 |

* inx=| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |*/

std::cout << str.find("a",0)<< std::endl;//0,find()返回的是從0位置起,第一次出現的位置

std::cout << str.find("a",1)<< std::endl;//6,

std::cout << str.find("abc")<< std::endl;//0,位置不填寫的話,預設是從0開始。

std::cout << str.rfind("a",8)<< std::endl;//6,rfind()返回的是從8位置起,向前查詢第一次出現

std::cout << str.rfind("a",2)<< std::endl;//0

std::cout << str.rfind("abc")<< std::endl;//6,位置不填寫的話,預設是從最後位置向前查詢開始。6

總結:位置標號是從0開始的;rfind是逆向開始查詢的;

find_first_of,find_last_of的應用環境,我們需要在str中,找到'a','h','5'中任何乙個最先出現的並返回下標。那麼利用find的做法是先查詢a,如果找到則返回乙個下標,再查詢h.....,最後比較所有下標,選出最小的乙個。是不是很麻煩?現在只需乙個函式。

std::string str1 = "ah5";

std::cout << str.find_first_of(str1,0)<< std::endl;//0

std::cout << str.find_last_of(str1,30)<< std::endl;//10

find_first_of:為順序查詢,在str中,a和5均出現了,但是a最先出現,所以返回0;

find_last_of:為逆序查詢,在str中,反向看的話5最先出現,所以返回10

擴充套件:

需求:要求返回str中包含a,h,5的所有位置下標。分析:因為find等所有函式返回的是第一次出現的小標,因此,當我們第一次得到下標時,接著在以該位置為起點繼續find。

int i = str.find_first_of(str1,0);

while(i != -1){

std::cout《功能:刪除字串中某個字元或字串

函式:erase(int pos,int n)/erase(iterator start,iterator end)

引數:過載函式有兩個,即是說有兩套引數。第一套:int pos為刪除字元下標,int n表示在pos開始連續刪除n個字元。例如:

str.erase(4,2):從下標為2的字元開始,連續刪除2個字元。第二套引數:注意為迭代器。一般str.begin();str.end()都是返回的迭代器。

第一套引數的使用(刪除位置4,即『2』開始的兩個字元,即『2』和『3』)

std::string str = "abc123abc456";

/* str=| a | b | c | 1 | 2 | 3 | a | b | c | 4 | 5 | 6 |

* inx=| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |*/

str.erase(4,2);//abc1abc456

std::string str = "abc123abc456";

/* str=| a | b | c | 1 | 2 | 3 | a | b | c | 4 | 5 | 6 |

* inx=| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |*/

str.erase(str.begin()+4,str.begin()+6);//abc1abc456

std::cout《實現與上面的同樣功能。注意為什麼要用begin,因為begin返回的是迭代器,而我們的入參必須是迭代器。功能,刪除4到6這段的字元(不包括6),當然還可以刪除某個位置上的字元,就只需傳入乙個引數就可以了。

功能:在str中特定位置插入字元或字串

函式:insert()

該函式有多個過載,只需要在erase函式中,搞明白迭代器,就很容易懂。

string &insert(int p0, const char *s);——在p0位置插入字串s

string &insert(int p0, const char *s, int n);——在p0位置插入字串s的前n個字元

string &insert(int p0,const string &s);——在p0位置插入字串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字串s從pos開始的連續n個字元

string &insert(int p0, int n, char c);//在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it處插入從first開始至last-1的所有字元

void insert(iterator it, int n, char c);//在it處插入n個字元c

功能:替換str中的某處或多處

函式:repalce()

MySQL學習筆記 增刪改查

有關資料庫的dml操作 insert into delete truncate update select 條件查詢 查詢排序 聚合函式 分組查詢 drop truncate delete delete刪除資料,保留表結構,可以回滾,如果資料量大,很慢,回滾就是因為備份刪除的資料 truncate刪...

Qt 學習筆記 TreeWidget 增刪改

在窗體上放乙個treewidget控制項和四個pushbutton加乙個horizontal spacer 布局如圖 給樹新增元素節點的方法和實現 件 qtreewidgetitem addtreeroot qstring name,qstring desc qtreewidgetitem addt...

SQL學習筆記 增刪改查

語句 insert into 表明 values 資料1 資料2 資料3 如果直接新增會在資料後面再新增一條資料 表名 test insert into test values test test 1 其實values後面加上的就是對應col的資料 當然也可以在指定的位置新增資料如 insert i...