跳到正文

Wiki

string容器

約 6 分鐘閱讀

本文由簡體中文內容確定性轉換,並受版本化術語表保護。

1.string容器

1.1.string基本概念

本質:

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

string和char * 區別:

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

特點:

string 類內部封裝了很多成員方法

例如:查詢find,複製copy,刪除delete 替換replace,插入insert

string管理char*所分配的記憶體,不用擔心複製越界和取值越界等,由類內部進行負責

1.2.string建構函式

建構函式原型:

  • string(); //建立一個空的字串 例如: string str; string(const char* s); //使用字串s初始化
  • string(const string& str); //使用一個string物件初始化另一個string物件
  • string(int n, char c); //使用n個字元c初始化

示例:

#include <iostream>
#include <string>

using namespace std;

//string构造
void test01()
{
	string s1; //创建空字符串,调用无参构造函数
	cout << "str1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str); //把c_string转换成了string

	cout << "str2 = " << s2 << endl;

	string s3(s2); //调用拷贝构造函数
	cout << "str3 = " << s3 << endl;

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

int main() {

	test01();


	return 0;
}

執行結果:

str1 =
str2 = hello world
str3 = hello world
str3 = hello world

總結:string的多種構造方式沒有可比性,靈活使用即可

1.3.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;

//赋值
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();


	return 0;
}

執行結果:

str1 = hello world
str2 = hello world
str3 = a
str4 = hello c++
str5 = hello
str6 = hello
str7 = xxxxx

總結:

​ string的賦值方式很多,operator= 這種方式是比較實用的

1.4.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個字元連線到字串結尾

示例:

#include <iostream>

using namespace std;

//字符串拼接
void test01()
{
	string str1 = "我";

	str1 += "爱玩游戏";

	cout << "str1 = " << str1 << endl;
	
	str1 += ':';

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

	string str2 = "LOL DNF";

	str1 += str2;

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

	string str3 = "I";
	str3.append(" love ");
	str3.append("game abcde", 4);
	//str3.append(str2);
	str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
	cout << "str3 = " << str3 << endl;
}
int main() {

	test01();


	return 0;
}

執行結果:

str1 = 我爱玩游戏
str1 = 我爱玩游戏:
str1 = 我爱玩游戏:LOL DNF
str3 = I love gameDNF

總結:字串拼接的過載版本很多,初學階段記住幾種即可

1.5.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

示例:

#include <iostream>

using namespace std;

//查找和替换
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";
	str1.replace(1, 3, "1111");

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

int main() {

	//test01();
	//test02();


	return 0;
}

執行結果: 程式正常結束,終端沒有輸出。

總結:

  • find查詢是從左往後,rfind從右往左
  • find找到字串後返回查詢的第一個字元位置,找不到返回-1
  • replace在替換時,要指定從哪個位置起,多少個字元,替換成什麼樣的字串

1.6.string字串比較

功能描述:

  • 字串之間的比較

比較方式:

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

= 返回 0

> 返回 1

< 返回 -1

函式原型:

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

示例:

#include <iostream>

using namespace std;

//字符串比较
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();


	return 0;
}

執行結果:

s1 大于 s2

總結:字串對比主要是用於比較兩個字串是否相等,判斷誰大誰小的意義並不是很大

1.7.string字元存取

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

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

示例:

#include <iostream>

using namespace std;

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();


	return 0;
}

執行結果:

h e l l o   w o r l d
h e l l o   w o r l d
xxllo world

總結:string字串中單個字元存取有兩種方式,利用 [ ] 或 at

1.8.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;

//字符串插入和删除
void test01()
{
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);  //从1号位置开始3个字符
	cout << str << endl;
}

int main() {

	test01();


	return 0;
}

執行結果:

h111ello
hello

**總結:**插入和刪除的起始下標都是從0開始

1.9.string子串

功能描述:

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

函式原型:

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

示例:

#include <iostream>

using namespace std;

//子串
void test01()
{

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

	string email = "hello@sina.com";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;

}

int main() {

	test01();


	return 0;
}

執行結果:

subStr = bcd
username: hello

**總結:**靈活的運用求子串功能,可以在實際開發中獲取有效的資訊