c++STL常用容器之String容器——全面總結(附案例解析)其中所用函數的聲明詳細解析。如:賦值函數assign()(十)

這裏有C++STL——全面總結詳細教程(附案例解析)(持續更新中)


目錄

string基本概念

string構造函數

string賦值操作

根據assign聲明的意思:

在s字符串中的a號位置開始,讓後面的n個字符,予以賦值操作。

string字符串拼接

string查找和替換

string字符串比較

string字符存取

string插入和刪除

string子串



string基本概念

本質:

  • string是C++風格的字符串,而string本質上是一個類

string和char * 區別:

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

特點:

  • string 類內部封裝了很多成員方法
  • 例如:查找find,拷貝copy,刪除delete 替換replace,插入insert
  • string管理char*所分配的內存,不用擔心複製越界和取值越界等,由類內部進行負責

 

string構造函數

構造函數原型:

  • string();             //創建一個空的字符串 例如: string str;

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

  • string(const string& str); //使用一個string對象初始化另一個string對象

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

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01() {
	string s1;
	cout << "str1= " << s1 << endl;

	const char* str = " hello world";
	string s2(str);
	cout << "str2= " << s2 << endl;

	string s3(s2);
	cout << "str3= " << s3 << endl;

	string s4(10, 'a');
	cout << "str4= " << s4 << endl;
}

int main() {
	test01();

	system("pause");
	return 0;
}

string賦值操作

功能描述:

  • 給string字符串進行賦值

賦值的函數原型:

  • string& operator=(const char* s);        //char*類型字符串 賦值給當前的字符串

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

  • string& operator=(char c);                    //字符賦值給當前的字符串

  • 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賦給當前字符串

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01() {
	string str1;
	str1 = "hello world";
	cout << "str1 = " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2 = " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3 = " << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4 = " << str4 << endl;

	string str5;
	str5.assign("hello c++", 5);
	cout << "str5 = " << str5 << endl;


	string str6;
	str6.assign(str5);
	cout << "str6 = " << str6 << endl;

	string str7;
	str7.assign(5, 'x');
	cout << "str7 = " << str7 << endl;
}

int main() {
	test01();

	system("pause");
	return 0;
}

總結:assign可以選擇字符中的特定的一段給予賦值。但在實際的編程中,”operator=“還是常見和實用的。

其中發現在

上面的代碼中:

assign都是以字符串形式出現的。如我我們以字符串變量形式。就發現問題。

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

中,我們在後面加上一個整形數據。

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

會使字符串中前n個字節消失,而賦值剩餘字符串中的內容。

從2號位開始,後面的都給賦值。

	string str4;
	str4.assign("hello c++");

	string str8;
	str8.assign(str4,2);
	cout << "str4 = " << str4 << endl;
	cout << "str8=" << str8 << endl;

str4=“hello c++”

str8=“llo c++”

//計算機從0開始所以第一個爲小寫的”L“而不是”e”

將2改爲6

	string str4;
	str4.assign("hello c++");

	string str8;
	str8.assign(str4,6);
	cout << "str4 = " << str4 << endl;
	cout << "str8=" << str8 << endl;

str8=“c++”

學習這個assign函數,我們可以去看這個函數是怎麼定義的,是一個比較好的學習辦法。

//選中函數Control+F12.或者右鍵轉到聲明。

我們轉到assign的聲明去看一下:

我們發現定義中

basic_string& assign(const basic_string& _Right, const size_type _Roff, size_type _Count = npos)

少了一個參數。

實際的

string& assign(const string &s,int n); 

應該爲:

string& assign(const string &s,int a,int n); 

根據assign聲明的意思:

在s字符串中的a號位置開始,讓後面的n個字符,予以賦值操作。

後面增加3

也就是從2開始,往後的3位字符予以賦值。

運行結果爲:

所以:如果後兩個參數都沒有,那就是全部賦值。如果只有一個n,那就是從n開始。有兩個就是區間。

注意:後面的整形要麼只有1個,要麼兩個都有,不能爲空!空會報錯。

 

string字符串拼接

功能描述:

  • 實現在字符串末尾拼接字符串

函數原型:

  • string& operator+=(const char* str);                       //重載+=操作符
  • string& operator+=(const char c);                            //重載+=操作符
  • string& operator+=(const string& str);       //重載+=操作符
  • string& append(const char *s);                                  //把字符串s連接到當前字符串結尾
  • string& append(const char *s, int n);                      //把字符串s的前n個字符連接到當前字符串結尾
  • string& append(const string &s);                               //同operator+=(const string& str)
  • string& append(const string &s, int pos, int n); //字符串s中從pos開始的n個字符連接到字符串結尾

可與加“字符串”、字符、變量變量相加。也可以用函數append去操作。和上面所說的assign()函數,沒什麼區別。

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01() {
	string str1 = "我";

	str1 += "愛學習";

	cout << "str1 = " << str1 << endl;

	str1 += ':';

	cout << "str1 = " << str1 << endl;

	string str2 = "c++ STL";

	str1 += str2;

	cout << "str1 = " << str1 << endl;

	string str3 = "I";
	str3.append(" love ");
	str3.append("c++ abcde", 3);

	str3.append(str2, 4, 3); // 從下標4位置開始 ,截取3個字符,拼接到字符串末尾
	cout << "str3 = " << str3 << endl;
}

int main() {
	test01();

	system("pause");
	return 0;
}

看一下函數聲明:

與上面的assign()函數差不多。就不再贅述。

 

string查找和替換

功能描述:

  • 查找:查找指定字符串是否存在
  • 替換:在指定的位置替換字符串

函數原型:

  • int find(const string& str, int pos = 0) const;         //查找str第一次出現位置,從pos開始查找
  • int find(const char* s, int pos = 0) const;        //查找s第一次出現位置,從pos開始查找
  • int find(const char* s, int pos, int n) const;           //從pos位置查找s的前n個字符第一次位置
  • int find(const char c, int pos = 0) const;         //查找字符c第一次出現位置
  • int rfind(const string& str, int pos = npos) const;  //查找str最後一次位置,從pos開始查找
  • int rfind(const char* s, int pos = npos) const;    //查找s最後一次出現位置,從pos開始查找
  • int rfind(const char* s, int pos, int n) const;          //從pos查找s的前n個字符最後一次位置
  • int rfind(const char c, int pos = 0) const;                 //查找字符c最後一次出現位置
  • string& replace(int pos, int n, const string& str);   //替換從pos開始n個字符爲字符串str
  • string& replace(int pos, int n,const char* s);            //替換從pos開始的n個字符爲字符串s

對於find()和rfind()函數聲明中的共同點和區別:

  • find()函數查找第一次出現。
  • rfind()函數查找最後一次出現。
  • 它們都是一個區間的操作。可選擇性的定義查找的區間。並且都是從前往後去查找。

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01(){
	//查找
	string str1 = "abcdefgde";

	int pos = str1.find("de");

	if (pos == -1){
		cout << "未找到" << endl;
	}
	else{
		cout << "pos = " << pos << endl;
	}

	pos = str1.rfind("de");
	cout << "pos = " << pos << endl;
}

void test02(){
	//替換
	string str1 = "abcdefgde";
	string str2 = "2222";
	str1.replace(1, 3, str2);
	//str1.replace(1, 3, "2222");
	cout << "str1 = " << str1 << endl;
}


int main() {
	test01();
	test02();
	system("pause");
	return 0;
}

這兩種都是一個結果

 

string字符串比較

功能描述:

  • 字符串之間的比較

比較方式:

  • 字符串比較是按字符的ASCII碼進行對比

= 返回 0

> 返回 1

< 返回 -1

 

函數原型:

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

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01(){

	string s1 = "hello";
	string s2 = "aello";

	int ret = s1.compare(s2);

	if (ret == 0) {
		cout << "s1 等於 s2" << endl;
	}
	else if (ret > 0){
		cout << "s1 大於 s2" << endl;
	}
	else{
		cout << "s1 小於 s2" << endl;
	}
}


int main() {
	test01();

	system("pause");
	return 0;
}

因爲a小於h。

 

 

string字符存取

string中單個字符存取方式有兩種

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

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01(){

	string str = "hello world";

	for (int i = 0; i < str.size(); i++){
		cout << str[i] << " ";
	}
	cout << endl;

	for (int i = 0; i < str.size(); i++){
		cout << str.at(i) << " ";
	}
	cout << endl;

	//字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
}


int main() {
	test01();

	system("pause");
	return 0;
}

 

 

string插入和刪除

功能描述:

  • 對string字符串進行插入和刪除字符操作

函數原型:

  • string& insert(int pos, const char* s);        //插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c);        //在指定位置插入n個字符c
  • string& erase(int pos, int n = npos);            //刪除從Pos開始的n個字符

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01(){
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);  //從1號位置開始3個字符
	cout << str << endl;
}


int main() {
	test01();

	system("pause");
	return 0;
}

 

string子串

功能描述:

  • 從字符串中獲取想要的子串

函數原型:

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

案例如下:

#include<iostream>
using namespace std;
#include<string>

void test01(){

	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "[email protected]";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;
}


int main() {
	test01();

	system("pause");
	return 0;
}

總結:子串對於信息的獲取還是非常重要的。靈活運用求子串功能,對於信息的獲取來說簡單方便。


本人水平有限,望各位看客批評指正!!!

以上就是STL——Sring容器的總結與概括了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章