免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
STL_string容器

一、string概念

string是STL的字符串類型,通常用來表示字符串。而在使用string之前,字符串通常是用char*表示的。string與char*都可以用來表示字符串,那么二者有什么區(qū)別。

string和char*的比較:

  • string是一個(gè)類, char*是一個(gè)指向字符的指針。

    ? string封裝了char*,管理這個(gè)字符串,是一個(gè)char*型的容器。

  • string不用考慮內(nèi)存釋放和越界。

    ? string管理char*所分配的內(nèi)存。每一次string的復(fù)制,取值都由string類負(fù)責(zé)維護(hù),不用擔(dān)心復(fù)制越界和取值越界等。

  • string提供了一系列的字符串操作函數(shù)

    ? 查找find,拷貝copy,刪除erase,替換replace,插入insert

//string 轉(zhuǎn) char*
string str_1="string";
const char* cstr_1=str.c_str();
//char* 轉(zhuǎn) string
char* cstr_2="char";
string str_2(cstr);

二、string的構(gòu)造函數(shù)

  1. 默認(rèn)構(gòu)造函數(shù):string(); //構(gòu)造一個(gè)空的字符串string s1。

  2. 構(gòu)造函數(shù):string(const string &str); //構(gòu)造一個(gè)與str一樣的string。如string s1(s2)。

  3. 帶參數(shù)的構(gòu)造函數(shù):

    string(const char *s); //用字符串s初始化

    string(int n,char c); //用n個(gè)字符c初始化

string s1; //調(diào)用無(wú)參構(gòu)造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3); //拷貝構(gòu)造

cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
/*
結(jié)果:

aaaaaaaaaa
abcdefg
abcdefg
*/

三、string的存取字符操作

string類的字符操作:

  • const char &operator[] (int n) const; //通過[]方式取字符
  • const char &at(int n) const; //通過at方法獲取字符
  • char &operator[] (int n);
  • char &at(int n);

operator[]和at()均返回當(dāng)前字符串中第n個(gè)字符,但二者是有區(qū)別的。

主要區(qū)別在于at()在越界時(shí)會(huì)拋出異常,[]在剛好越界時(shí)會(huì)返回(char)0,再繼續(xù)越界時(shí),編譯器直接出錯(cuò)。如果你的程序希望可以通過try,catch捕獲異常,建議采用at()。

string s1 = "abcdefg";

//重載[]操作符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl;

//at成員函數(shù)
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl;

//區(qū)別:[]方式 如果訪問越界,直接掛了
//at方式 訪問越界 拋異常out_of_range

try {
//cout << s1[100] << endl;
cout << s1.at(100) << endl;
}
catch (...) {
cout << "越界!" << endl;
}
/*
結(jié)果:
a b c d e f g
a b c d e f g
越界!
*/

四、從string取得const char*的操作

const char *c_str() const; //返回一個(gè)以'\0'結(jié)尾的字符串的首地址

//string 轉(zhuǎn) char*
string str_1="string";
const char *cstr_1=str.c_str();

五、把string拷貝到char*指向的內(nèi)存空間的操作

int copy(char *s, int n, int pos=0) const;

把當(dāng)前串中以pos開始的n個(gè)字符拷貝到以s為起始位置的字符數(shù)組中,返回實(shí)際拷貝的數(shù)目。注意要保證s所指向的空間足夠大以容納當(dāng)前字符串,不然會(huì)越界。

string s1 = "abcdefg";
int n = 5;pose
int pose = 3;

char* s2 = (char*)malloc(n*sizeof(char));
if(s1.copy(s2, n, pose))
cout << s2 ;
else cout<<"error"<<endl;
/*
結(jié)果:
def
*/

六、string的長(zhǎng)度

int length() const; //返回當(dāng)前字符串的長(zhǎng)度。長(zhǎng)度不包括字符串結(jié)尾的'\0'。

bool empty() const; //當(dāng)前字符串是否為空

string s1 = "abcdefg";
string s2 = "";

int s1_length=s1.length();  
bool s1_empty = s1.empty();
int s2_length = s2.length();  
bool s2_empty = s2.empty();

cout << s1_length << "     " << s1_empty << endl;
cout << s2_length << "     " << s2_empty << endl;
/*
結(jié)果:
7     0
0     1
*/

七、string的賦值

string &operator=(const string &s);//把字符串s賦給當(dāng)前的字符串

string &assign(const char *s); //把字符串s賦給當(dāng)前的字符串

string &assign(const char *s, int n); //把字符串s的前n個(gè)字符賦給當(dāng)前的字符串

string &assign(const string &s); //把字符串s賦給當(dāng)前字符串

string &assign(int n,char c); //用n個(gè)字符c賦給當(dāng)前字符串

string &assign(const string &s,int start, int n); //把字符串s中從start開始的n個(gè)字符賦給當(dāng)前字符串

string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl;

//成員方法assign
s1.assign("jkl");
cout << s1 << endl;
/*
結(jié)果:
abcdef
appp
a
jkl
*/

八、string字符串連接

string &operator+=(const string &s); //把字符串s連接到當(dāng)前字符串結(jié)尾

string &operator+=(const char *s);//把字符串s連接到當(dāng)前字符串結(jié)尾

string &append(const char *s); //把字符串s連接到當(dāng)前字符串結(jié)尾

string &append(const char *s,int n); //把字符串s的前n個(gè)字符連接到當(dāng)前字符串結(jié)尾

string &append(const string &s); //同operator+=()

string &append(const string &s,int pos, int n);//把字符串s中從pos開始的n個(gè)字符連接到當(dāng)前字符串結(jié)尾

string &append(int n, char c); //在當(dāng)前字符串結(jié)尾添加n個(gè)字符c

string s = "abcd";
string s2 = "1111";
s += "abcd";
s += s2;
cout << s << endl;

string s3 = "2222";
s2.append(s3);
cout << s2 << endl;

string s4 = s2 + s3;
cout << s4 << endl;
/*
結(jié)果:
abcdabcd1111
11112222
111122222222
*/

九、string的比較

int compare(const string &s) const; //與字符串s比較

int compare(const char *s) const; //與字符串s比較

compare函數(shù)在>時(shí)返回 1,<時(shí)返回 -1,==時(shí)返回 0。比較區(qū)分大小寫,比較時(shí)參考字典順序,排越前面的越小。大寫的A比小寫的a小。

string s1 = "abcd";
string s2 = "abce";
if (s1.compare(s2)==0) {
cout << "字符串相等!" << endl;
}
else {
cout << "字符串不相等!" << endl;
}

十、string的子串

string substr(int pos=0, int n=npos) const; //返回由pos開始的n個(gè)字符組成的子字符串

string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
/*
結(jié)果:
bcd
*/

十一、string的查找和替換

查找

int find(char c,int pos=0) const; //從pos開始查找字符c在當(dāng)前字符串的位置

int find(const char *s, int pos=0) const; //從pos開始查找字符串s在當(dāng)前字符串的位置

int find(const string &s, int pos=0) const; //從pos開始查找字符串s在當(dāng)前字符串中的位置

//find函數(shù)如果查找不到,就返回-1

int rfind(char c, int pos=npos) const; //從pos開始從后向前查找字符c在當(dāng)前字符串中的位置

int rfind(const char *s, int pos=npos) const;

int rfind(const string &s, int pos=npos) const;

//rfind是反向查找的意思,如果查找不到, 返回-1

替換

string &replace(int pos, int n, const char *s);//刪除從pos開始的n個(gè)字符,然后在pos處插入串s

string &replace(int pos, int n, const string &s); //刪除從pos開始的n個(gè)字符,然后在pos處插入串s

void swap(string &s2); //交換當(dāng)前字符串與s2的值

// 字符串的查找和替換
    string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
    size_t index = s1.find("wbm", 0);
    cout << "index: " << index;  

    //求itcast出現(xiàn)的次數(shù)
    size_t offindex = s1.find("wbm", 0);
    while (offindex != string::npos){

         cout << "在下標(biāo)index: " << offindex << "找到wbm\n";
         offindex = offindex + 1;
         offindex = s1.find("wbm", offindex);
    } 

    //替換 
    string s2 = "wbm hello wbm 111 wbm 222 wbm 333";
    s2.replace(0, 3, "wbm");
    cout << s2 << endl; 

    //求itcast出現(xiàn)的次數(shù)
    offindex = s2.find("wbm", 0);
    while (offindex != string::npos){

         cout << "在下標(biāo)index: " << offindex << "找到wbm\n";
         s2.replace(offindex, 3, "WBM");
         offindex = offindex + 1;
         offindex = s1.find("wbm", offindex);
    }
    cout << "替換以后的s2:" << s2 << endl; 
/*
結(jié)果:
index: 0在下標(biāo)index: 0找到wbm
在下標(biāo)index: 10找到wbm
在下標(biāo)index: 18找到wbm
在下標(biāo)index: 26找到wbm
wbm hello wbm 111 wbm 222 wbm 333
在下標(biāo)index: 0找到wbm
在下標(biāo)index: 10找到wbm
在下標(biāo)index: 18找到wbm
在下標(biāo)index: 26找到wbm
替換以后的s2:WBM hello WBM 111 WBM 222 WBM 333
*/

十二、String的區(qū)間刪除和插入

string &insert(int pos, const char *s);

string &insert(int pos, const string &s);//在pos位置插入字符串s

string &insert(int pos, int n, char c); //在pos位置 插入n個(gè)字符c

string &erase(int pos=0, int n=npos); //刪除pos開始的n個(gè)字符,返回修改后的字符串

string s = "abcdefg";
s.insert(3, "111");
cout << s << endl;

s.erase(0, 2);
cout << s << endl;
/*
結(jié)果:
abc111defg
c111defg
*/
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
大數(shù)相乘
C學(xué)習(xí)心得
字符串比較函數(shù) strcmp,strncmp 源碼
C++中cout輸出字符型指針地址值的方法
CrackCode 題目整理 (第一章)
csdn(18條消息) 2021版C++教程,C++進(jìn)階教程(最全C++教程)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服