




已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
c+中的string常用函數(shù)用法basic_string:append 向string 的后面加字符或字符串。(比+=, push_back 更靈活)(1)向string 的后面加C-stringbasic_string& append( const value_type* _Ptr );string s ( Hello ); / s=”Hello ”const char *c = Out There ;s.append ( c ); / s=”Hello Out There”(2)向string 的后面加C-string 的一部分basic_string& append( const value_type* _Ptr, size_type _Count );string s ( Hello ); / s=”Hello ”const char *c = Out There ;s.append ( c , 3 ); / s=”Hello Out”(3)向string 的后面加string(有兩種方法)basic_string& append( const basic_string& _Str );string s1 ( Hello ), s2 ( Wide ), s3( World );s1.append ( s2 ); / s1=”Hello Wide”s1 += s3; / s1=”Hello Wide World”(4)向string 的后面加string 的一部分 -Abasic_string& append( const basic_string& _Str, size_type _Off,size_type _Count );string s1 ( Hello ), s2 ( Wide World );s1.append ( s2 , 5 , 5 ); / s1=”Hello World”(5)向string 的后面加string 的一部分 -Btemplate basic_string& append(InputIterator _First, InputIterator _Last );string str1f ( Hello ), str2f ( Wide World );str1f.append ( str2f.begin ( ) + 5 , str2f.end ( ) );/ s1=”Hello World”(6)向string 的后面加多個字符basic_string& append( size_type _Count, value_type _Ch );string str1e ( Hello );str1e.append ( 4 , ! ); / s1=”Hello !”basic_string:assign給string 賦值。 (比“=”更靈活)(1)向string 賦C-stringbasic_string& assign( const value_type* _Ptr );string s;const char *c = Out There;s.assign ( c ); / s=”O(jiān)ut There”(2)向string 賦C-string 的一部分basic_string& assign( const value_type* _Ptr, size_type _Count );string s;const char *c = Out There;s.assign ( c , 3 ); / s=”O(jiān)ut”(3)向string 賦string(有兩種方法)basic_string& assign( const basic_string& _Str );string s1 ( Hello ), s2 ( Wide ), s3( World );s1.assign ( s2 ); / s1=”Wide”s1 = s3; / s1=”World”(4)向string 賦string 的一部分 -Abasic_string& assign( const basic_string& _Str, size_type off,size_type _Count );string s1 ( Hello ), s2 ( Wide World );s1.assign ( s2 , 5 , 5 ); / s1=”World”(5)向string 賦string 的一部分 -Btemplate basic_string& assign(InputIterator _First,InputIterator _Last );string str1f ( Hello ), str2f ( Wide World );str1f.assign ( str2f.begin ( ) + 5 , str2f.end ( ) ); / str1f=”World”(6)向string 賦 多個字符basic_string& assign( size_type _Count, value_type _Ch );string str1e ( Hello );str1e.assign ( 4 , ! ); / s1=”!”basic_string:compare如果所比較的兩個string 相等,則返回0; 操作string 大于參數(shù)string,返回正數(shù);操作string 小于參數(shù)string,返回負數(shù)。(1)比較操作string 與_Str 或C-string_Ptrint compare( const basic_string& _Str ) const;int compare( const value_type* _Ptr ) const;int com = pare ( sp );(2)比較操作string 中_Pos1(下標)開始的_Num1 個字符 與 string_Str比較操作string 中_Pos1(下標)開始的_Num1 個字符 與 C-string _Ptr比較操作string 中Pos1(下標)開始的Num1 個字符 與Str 中Off(下標)開始Count 個字符int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str );int compare( size_type _Pos1, size_type _Num1, const value_type* _Ptr ) const;int compare( size_type _Pos1, size_type _Num1, const basic_string& _Str,size_type _Off, size_type _Count );int com1 = pare ( 2 , 3 , sp );int com2 = pare ( 2 , 3 , c );int com3 = pare ( 1 , 3 , cs , 3 ,1 );basic_string:erase刪除string 中的一個或幾個元素。前兩個成員函數(shù),返回要被刪除的子串的下一個元素的iterator; 第三個函數(shù),返回刪除后的string 的引用。(1)刪除string 中從_First 到_Last 的字符iterator erase( iterator _First, iterator _Last );basic_string :iterator s_Iter;s_Iter = s.erase ( s.begin ( ) + 3 , s.end ( ) - 1 ); / s_Iter=s.end( )(2) 刪除string 中_It 所指的字符iterator erase( iterator _It );s_Iter = s.erase ( s.begin ( ) + 5 );(3) 刪除string 中從_Pos(下標)開始的_Count 個字符basic_string& erase( size_type _Pos = 0, size_type _Count = npos );str = s.erase ( 6 , 8 ); / str 也是stringbasic_string:find尋找給定的string。返回找到的第一個string 下標值;如果沒找到則返回npos。(1)找一個character_Ch。(默認從頭找)size_type find( value_type _Ch, size_type _Off = 0 ) const;string s ( Hello Everyone );basic_string :size_type index1, index2;static const basic_string :size_type npos = -1;index1 = s.find ( e , 3 ); / index1=8,不是6index2 = s.find ( x ); / index2=-1if (indexCh1a != npos ) cout indexCh1a endl;else cout The character e was not found in str1 . endl;(2)找一個C-string。(默認從頭找)size_type find( const value_type* _Ptr, size_type _Off = 0 ) const;string s ( Let me make this perfectly clear. );basic_string :size_type index;const char *c = perfect;index = s.find ( c , 5 ); / index=17(3)找一個string。(默認從頭找)size_type find( const basic_string& _Str, size_type _Off = 0 ) const;string s ( clearly this perfectly unclear. );basic_string :size_type index;string sta ( clear );index = s.find ( sta , 5 ); / index=24basic_string:max_size返回string 能放的最大元素個數(shù)。(不同于capacity)size_type max_size( ) const;basic_string :size_type cap, max;cap = s.capacity ( );max = s.max_size ( ); / max=4294967294.basic_string:rfind尋找給定的string。返回找到的第一個string 下標值;如果沒找到則返回npos。與find 不同的是:rfind 默認從npos 開始找。其他相同。basic_string:replace將原string 中的元素或子串替換。返回替換后的string。(1)用string 或C-string 代替操作string 中從_Pos1 開始的_Num1 個字符basic_string& replace( size_type _Pos1,size_type _Num1, const value_type* _Ptr);basic_string& replace(size_type _Pos1,size_type _Num1,const basic_string_Str);string a,b;string s ( AAAAAAAA );string s1p ( BBB );const char* cs1p = CCC;a = s.replace ( 1 , 3 , s1p ); / s=”ABBBAAAA”b = s.replace ( 5 , 3 , cs1p ); / s=”ABBBACCC”(2)用string 中從_Pos2 開始的_Num2 個字符,代替操作string 中從_Pos1 開始的_Num1 個字符用C-string 中的_Num2 個字符,代替操作string 中從_Pos1 開始的_Num1 個字符basic_string& replace( size_type _Pos1, size_type _Num1, const basic_string& _Str,size_type _Pos2, size_type );basic_string& replace( size_type _Pos1, size_type _Num1,const value_type* _Ptr, size_type _Num2 );string a, b;string s ( AAAAAAAA );string s2p ( BBB );const char* cs2p = CCC;a = s.replace ( 1 , 3 , s2p , 1 , 2 ); / s=”ABBAAAA”b = s.replace ( 4 , 3 , cs2p , 1 ); / s=”ABBAC”(3)用_Count 個character_Ch ,代替操作string 中從_Pos1 開始的_Num1 個字符basic_string& replace( size_type _Pos1, size_type _Num1,size_type_Count, value_type_Ch );string result;string s ( AAAAAAAA );char ch = C;result = s.replace ( 1 , 3 , 4 , ch ); / s=”ACCCCAAAA”(4)用string 或C-string ,代替操作string 中從First0 到Last0 的字符basic_string&replace(iterator First0,iterator Last0, const basic_string& _Str);basic_string&replace(iterator First0,iterator _Last0, const value_type* _Ptr);string s ( AAAAAAAA ); string s4p ( BBB );const char* cs4p = CCC;basic_string:iterator IterF0, IterL0;IterF0 = s.begin ( ); IterL0 = s.begin ( ) + 3;string a, b;a = s.replace ( IterF0 , IterL0 , s4p ); / s=”BBBAAAAA”b = s.replace ( IterF0 , IterL0 , cs4p ); / s=”CCCAAAAA”(5)用string 中從_Pos2 開始的_Num2 個字符,代替操作string 中從First0 到Last0 的字符用C-string 中的_Num2 個字符,代替操作string 中從First0 到Last0 的字符basic_string& replace( iterator _First0, iterator _Last0,const value_type* _Ptr, size_type _Num2 );template basic_string& replace(iterator _First0, iterator _Last0,InputIterator _First, InputIterator _Last );IterF3 = s.begin ( ) + 1; IterL3 = s.begin ( ) + 3;IterF4 = s.begin ( ); IterL4 = s.begin ( ) + 2;a = s.replace ( IterF3 , IterL3 , IterF4 , IterL4 );b = s.replace ( IterF1 , IterL1 , cs5p , 4 );(6)用_Count 個character_Ch ,代替操作string 中從First0 到Last0 的字符basic_string& replace( iterator _First0, iterator _Last0,size_type _Count , value_type _Ch );a = s.replace ( IterF2 , IterL2 , 4 , ch );basic_string:swap交換兩個string。void swap( basic_string& _Str );s1.swap ( s2 );basic_string:substr返回從_Off(下標)開始的_Count 個字符組成的stringbasic_string substr( size_type _Off = 0, size_type _Count = npos ) const;string s(I love you!), sub;sub=s.substr( ); / sub=”I love you!”sub=s.substr(1); / sub=” love you!”sub=s.substr(3,4); / sub=”ove ”string函數(shù)用法函數(shù)名稱: strdup 函數(shù)原型: char *strdup(const char *s) 函數(shù)功能: 字符串拷貝,目的空間由該函數(shù)分配 函數(shù)返回: 指向拷貝后的字符串指針 參數(shù)說明: src-待拷貝的源字符串 所屬文件: #include #include #include int main() char *dup_str, *string=abcde; dup_str=strdup(string); printf(%s, dup_str); free(dup_str); return 0; 函數(shù)名稱: strcpy 函數(shù)原型: char* strcpy(char* str1,char* str2); 函數(shù)功能: 把str2指向的字符串拷貝到str1中去 函數(shù)返回: 返回str1,即指向str1的指針 參數(shù)說明: 所屬文件: #include #include int main() char string10; char *str1=abcdefghi; strcpy(string,str1); printf(the string is:%sn,string); return 0; 函數(shù)名稱: strncpy 函數(shù)原型: char *strncpy(char *dest, const char *src,int count) 函數(shù)功能: 將字符串src中的count個字符拷貝到字符串dest中去 函數(shù)返回: 指向dest的指針 參數(shù)說明: dest-目的字符串,src-源字符串,count-拷貝的字符個數(shù) 所屬文件: #include #include int main() char string10; char *str1=abcdefghi; strncpy(string,str1,3); string3=0; printf(%s,string); return 0; 函數(shù)名稱: strcat 函數(shù)原型: char* strcat(char * str1,char * str2); 函數(shù)功能: 把字符串str2接到str1后面,str1最后的0被取消 函數(shù)返回: str1 參數(shù)說明: 所屬文件: #include #include int main() char buffer80; strcpy(buffer,Hello ); strcat(buffer,world); printf(%sn,buffer); return 0; 函數(shù)名稱: strncat 函數(shù)原型: char *strncat(char *dest, const char *src, size_t maxlen) 函數(shù)功能: 將字符串src中前maxlen個字符連接到dest中 函數(shù)返回: 參數(shù)說明: 所屬文件: #include #include char buffer80; int main() strcpy(buffer,Hello ); strncat(buffer,world,8); printf(%sn,buffer); strncat(buffer,*,4); printf(%sn,buffer); return 0; 函數(shù)名稱: strcmp 函數(shù)原型: int strcmp(char * str1,char * str2); 函數(shù)功能: 比較兩個字符串str1,str2. 函數(shù)返回: str1str2,返回正數(shù). 參數(shù)說明: 所屬文件: #include #include int main() char *buf1=aaa, *buf2=bbb, *buf3=ccc; int ptr; ptr=strcmp(buf2, buf1); if(ptr0) printf(buffer 2 is greater than buffer 1n); else printf(buffer 2 is less than buffer 1n); ptr=strcmp(buf2, buf3); if(ptr0) printf(buffer 2 is greater than buffer 3n); else printf(buffer 2 is less than buffer 3n); return 0; 函數(shù)名稱: strncmp 函數(shù)原型: int strncmp(char *str1,char *str2,int count) 函數(shù)功能: &n bsp; 對str1和str2中的前count個字符按字典順序比較 函數(shù)返回: 小于0:str1str2 參數(shù)說明: str1,str2-待比較的字符串,count-比較的長度 所屬文件: #include #include int main() int ptr; char *buf1=aaabbb,*buf2=bbbccc,*buf3=ccc; ptr=strncmp(buf2,buf1,3); if (ptr0) printf(buffer 2 is greater than buffer 1); else printf(buffer 2 is less than buffer 1); ptr=strncmp(buf2,buf3,3); if (ptr0) printf(buffer 2 is greater than buffer 3); else printf(buffer 2 is less than buffer 3); return(0); 函數(shù)名稱: strpbrk 函數(shù)原型: char *strpbrk(const char *s1, const char *s2) 函數(shù)功能: 得到s1中第一個“同時也出現(xiàn)在s2中”字符的位置指針 函數(shù)返回: 位置指針 參數(shù)說明: 所屬文件: #include #include int main() char *p=Find all vowels; while(p) printf(%sn,p); p=strpbrk(p+1,aeiouAEIOU); return 0; 函數(shù)名稱: strcspn 函數(shù)原型: int strcspn(const char *s1, const char *s2) 函數(shù)功能: 統(tǒng)計s1中從頭開始直到第一個“來自s2中的字符”出現(xiàn)的長度 函數(shù)返回: 長度 參數(shù)說明: 所屬文件: #inc
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 個人公司合同范本
- 租賃房屋合同范本續(xù)租
- 室內(nèi)水泥漆合同范本
- 賓館培訓(xùn)會議合同范本
- 6的乘法口訣(教學(xué)設(shè)計)-2024-2025學(xué)年人教版數(shù)學(xué)二年級上冊
- 8 冀中的地道戰(zhàn)(教學(xué)設(shè)計)-2024-2025學(xué)年統(tǒng)編版語文五年級上冊
- 9 我心中的“110”(教學(xué)設(shè)計)統(tǒng)編版道德與法治三年級上冊
- 2025年順酐酸酐衍生物項目發(fā)展計劃
- 1《神州謠》教學(xué)設(shè)計-2023-2024學(xué)年語文二年級下冊統(tǒng)編版
- 托管班合作協(xié)議書
- 六年級語文-文言文閱讀訓(xùn)練題50篇-含答案-全國通用
- 出版物網(wǎng)絡(luò)零售備案表
- 云南省昭通市各縣區(qū)鄉(xiāng)鎮(zhèn)行政村村莊村名居民村民委員會明細
- 國家留學(xué)基金委國外大學(xué)邀請函
- QES三體系內(nèi)審檢查表 含審核記錄
- 信息論與編碼 自學(xué)報告
- 二年級乘除法口訣專項練習(xí)1000題-推薦
- 貸款項目資金平衡表
- 設(shè)計文件簽收表(一)
- 義務(wù)教育語文課程標準2022年版
- 公務(wù)員入職登記表
評論
0/150
提交評論