STL_string容器

1、string概念

string是STL的字符串類型,一般用來表示字符串。而在使用string以前,字符串一般是用char*表示的。string與char*均可以用來表示字符串,那麼兩者有什麼區別。數組

string和char*的比較:app

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

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

  • string不用考慮內存釋放和越界。code

    ​ string管理char*所分配的內存。每一次string的複製,取值都由string類負責維護,不用擔憂複製越界和取值越界等。內存

  • string提供了一系列的字符串操做函數字符串

    ​ 查找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);

2、string的構造函數

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

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

  3. 帶參數的構造函數:

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

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

string s1; //調用無參構造
	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
*/

3、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個字符,但兩者是有區別的。

主要區別在於at()在越界時會拋出異常,[]在恰好越界時會返回(char)0,再繼續越界時,編譯器直接出錯。若是你的程序但願能夠經過try,catch捕獲異常,建議採用at()。

string s1 = "abcdefg";

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

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

	//區別:[]方式 若是訪問越界,直接掛了
	//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
越界!
*/

4、從string取得const char*的操做

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

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

5、把string拷貝到char*指向的內存空間的操做

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

把當前串中以pos開始的n個字符拷貝到以s爲起始位置的字符數組中,返回實際拷貝的數目。注意要保證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
*/

6、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
*/

7、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
*/

8、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
*/

9、string的比較

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

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

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

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

10、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
*/

11、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函數若是查找不到,就返回-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出現的次數
    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出現的次數
    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
*/

12、String的區間刪除和插入

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
*/
相關文章
相關標籤/搜索