小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

STL_string容器

 頭號碼甲 2021-05-15

一、string概念

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

string和char*的比較:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
/*
結果:

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()均返回當前字符串中第n個字符,但二者是有區(qū)別的。

主要區(qū)別在于at()在越界時會拋出異常,[]在剛好越界時會返回(char)0,再繼續(xù)越界時,編譯器直接出錯。如果你的程序希望可以通過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;
}
/*
結果:
a b c d e f g
a b c d e f g
越界!
*/

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

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

//string 轉 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;

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

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;
/*
結果:
def
*/

六、string的長度

int length() const; //返回當前字符串的長度。長度不包括字符串結尾的'\0'。

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

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;
/*
結果:
7     0
0     1
*/

七、string的賦值

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

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

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

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

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

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

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;
/*
結果:
abcdef
appp
a
jkl
*/

八、string字符串連接

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

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

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

string &append(const char *s,int n); //把字符串s的前n個字符連接到當前字符串結尾

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

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

string &append(int n, char c); //在當前字符串結尾添加n個字符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;
/*
結果:
abcdabcd1111
11112222
111122222222
*/

九、string的比較

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

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

compare函數(shù)在>時返回 1,<時返回 -1,==時返回 0。比較區(qū)分大小寫,比較時參考字典順序,排越前面的越小。大寫的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個字符組成的子字符串

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

十一、string的查找和替換

查找

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

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

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

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

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

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個字符,然后在pos處插入串s

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

void swap(string &s2); //交換當前字符串與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 << "在下標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 << "在下標index: " << offindex << "找到wbm\n";
         s2.replace(offindex, 3, "WBM");
         offindex = offindex + 1;
         offindex = s1.find("wbm", offindex);
    }
    cout << "替換以后的s2:" << s2 << endl; 
/*
結果:
index: 0在下標index: 0找到wbm
在下標index: 10找到wbm
在下標index: 18找到wbm
在下標index: 26找到wbm
wbm hello wbm 111 wbm 222 wbm 333
在下標index: 0找到wbm
在下標index: 10找到wbm
在下標index: 18找到wbm
在下標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個字符c

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

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

s.erase(0, 2);
cout << s << endl;
/*
結果:
abc111defg
c111defg
*/

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多