欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++標準庫介紹及使用string類的詳細過程

 更新時間:2024年08月15日 16:30:58   作者:清風~徐~來  
C++中將string封裝為單獨的類,string?類是?C++?標準庫中的一個非常重要的類,用于表示和操作字符串,這篇文章主要介紹了C++標準庫介紹及使用string類,需要的朋友可以參考下

一.string類介紹

  C語言中,字符串是以’\0’結尾的一些字符的集合,為了操作方便,C標準庫中提供了一些str系列的庫函數(shù),但是這些庫函數(shù)與字符串是分離開的,不太符合OOP的思想,而且底層空間需要用戶自己管理,稍不留神可能還會越界訪問。
  C++中將string封裝為單獨的類,string 類是 C++ 標準庫中的一個非常重要的類,用于表示和操作字符串。string類位于命名空間std(標準庫)下,使用string類記得加上頭文件#include,并且使用命名空間using namespace std或者using std::string。

//使用時記得加上
#include<string>
using std::string;//或者using namespace std;
//源代碼大致框架
namespace std
{
	class string
	{
	private:
		char* _str;
		size_t _size;
		size_t _capacity;
	};
}

二.string類的靜態(tài)成員變量

靜態(tài)成員變量:static const size_t npos = -1;

int main()
{
	//static const size_t npos = -1;
	//typedef unsigned long long size_t
	//-1的原碼:10000000 00000000 00000000 00000001
	//-1的反碼:11111111 11111111 11111111 11111110
	//-1的補碼:11111111 11111111 11111111 11111111
	//npos:-1的補碼按照無符號整形打印出的值,由于靜態(tài)成員變量,類外使用時加上類域
	cout << string::npos << endl;//32位環(huán)境:4294967295
	return 0;
}

三.string類的常用接口

1.構造函數(shù)(constructor)

  • 無參構造string(); 構造空的string類對象,即空字符串。常用。
  • 有參構造string (const char* s); 用常量字符串來構造string類對象常用。
  • 拷貝構造string (const string& str); 用str拷貝構造string類對象常用。
  • string (const string& str, size_t pos, size_t len = npos); 構造從下標pos開始,長度為len的子串,含缺省參數(shù)npos。
  • string (const char* s, size_t n); 構造前n個字符組成的子串。string (size_t n, char c); 構造n個字符c組成的字符串。
int main()
{
	string s1;
	string s2("hello xzy");
	string s3(s2);
	string s4(s2, 6, 8);
	string s5("hello xzy", 5);
	string s6(10, 'x');
	cout << s1 << endl;//輸出:
	cout << s2 << endl;//輸出:hello xzy
	cout << s3 << endl;//輸出:hello xzy
	cout << s4 << endl;//輸出:xzy
	cout << s5 << endl;//輸出:hello
	cout << s6 << endl;//輸出:xxxxxxxxxx
	return 0;
}

2.析構函數(shù)(destructor)

~string(); 程序結束前自動調用,釋放堆區(qū)動態(tài)開辟的資源

3.運算符重載(operator)

1.operator=

  • string& operator= (const string& str); 常用。
  • string& operator= (const char* s);
  • string& operator= (char c);
int main()
{
	string s1;
	string s2;
	string s3;
	//賦值重載
	s1 = "hello xzy";
	s2 = s1;
	s3 = 'v';
	//拷貝構造
	string s4 = s1;
	cout << s1 << endl;//輸出:hello xzy
	cout << s2 << endl;//輸出:hello xzy
	cout << s3 << endl;//輸出:v
	cout << s4 << endl;//輸出:hello xzy
	return 0;
}

2.operator[]

char& operator[] (size_t pos); 返回字符引用,用于下標訪問,且可以修改。常用。

int main()
{
	string s1("hello xzy");
	s1[6] = 'w';
	s1[7] = 'j';
	s1[8] = '\0';
	cout << s1 << endl;//輸出:hello wj
	s1[10] = 'A';//下標越界,內部斷言assert報錯
	return 0;
}

3.operator+=

string& operator+= (const string& str); 常用。

string& operator+= (const char* s);

string& operator+= (char c);

int main()
{
	string s1("hello xzy");
	string s2(" how are you");
	s1 += s2;
	cout << s1 << endl;
	s1 += "???";
	cout << s1 << endl;
	s1 += '!';
	cout << s1 << endl;
	return 0;
}

4.operator+

string operator+ (const string& lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const char* lhs, const string& rhs);

int main()
{
	string s1("hello");
	string s2 = s1 + " world";
	string s3 = "xzy " + s1;
	string s4 = s2 + s3;
	cout << s2 << endl; //hello world
	cout << s3 << endl; //xzy hello
	cout << s4 << endl; //hello worldxzy hello
	return 0;
}

4.string的四種迭代器(iterator)

  迭代器是一種用于遍歷容器元素的對象(并非類,而是設計模式中的一種行為模式),它提供了一種通用的訪問容器元素的方式,無論容器的類型和數(shù)據(jù)結構如何。迭代器在C++標準庫中被廣泛使用,特別是在處理如vector、list、map等容器時。

1.正向迭代器 iterator

返回正向迭代器:可以修改字符串。

  • iterator begin(); 返回字符串的第一個字符。
  • iterator end(); 返回字符串最后一個有效字符(不含\0)的下一個字符。

2.反向迭代器 reverse_iterator

返回反向迭代器:可以修改字符串。

  • reverse_iterator rbegin(); 返回字符串最后一個有效字符(不含\0)。
  • reverse_iterator rend(); 返回字符串第一個字符的前一個字符。

3.const修飾的正向迭代器 const_iterator

返回const修飾的正向迭代器:不可以修改字符串。

const_iterator begin() const;

const_iterator end() const;

4.const修飾的反向迭代器 const_reverse_iterator

返回const修飾的反向迭代器:不可以修改字符串。

const_reverse_iterator rbegin() const;

const_reverse_iterator rend() const;

5.四種迭代器源代碼

int main()
{
	string s1("hello xzy");
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	string s2("hello xzy");
	string::reverse_iterator rit = s2.rbegin();
	while (rit != s2.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	const string s3("hello xzy");
	string::const_iterator cit = s3.begin();
	while (cit != s3.end())
	{
		//*cit += 2; s3不能修改
		cout << *cit << " ";
		++cit;
	}
	cout << endl;
	const string s4("hello xzy");
	string::const_reverse_iterator crit = s4.rbegin();
	while (crit != s4.rend())
	{
		//*crit += 2; s4不能修改
		cout << *crit << " ";
		++crit;
	}
	cout << endl;
	return 0;
}

5.string類對象的容量操作

  • size_t size() const; 返回字符串有效字符長度(不包括\0)。常用。
  • size_t length() const; 返回字符串有效字符長度(不包括\0)。
  • size_t capacity() const; 返回空間總大小(不包括\0)。常用。
  • void resize (size_t n); 為字符串預留大于等于n的空間(不包括\0),避免擴容,提高效率。常用。
  • void clear(); 清空數(shù)據(jù),但是一般不清容量。常用。
  • bool empty() const; 判斷是否為空。常用。void resize (size_t n); 只保留前n個數(shù)據(jù)。void resize (size_t n, char c); 保留前n個數(shù)據(jù),若n大于容量,后面用字符c補上。

注意:

  • size()與length()方法底層實現(xiàn)原理完全相同,引入size()的原因是為了與其他容器的接口保持一致,一般情況下基本都是用size()。
  • clear()只是將string中有效字符清空,不改變底層空間大小。
  • resize(size_t n) 與 resize(size_t n, char c)都是將字符串中有效字符個數(shù)改變到n個,不同的是當字符個數(shù)增多時:resize(n)用 ‘\0’ 來填充多出的元素空間,resize(size_t n, char c)用字符c來填充多出的元素空間。注意:resize在改變元素個數(shù)時,如果是將元素個數(shù)增多,可能會改變底層容量的大小,如果是將元素個數(shù)減少,底層空間總大小不變。
  • reserve(size_t res_arg=0):為string預留空間,不改變有效元素個數(shù),當reserve的參數(shù)小于string的底層空間總大小時,reserver不會改變容量大小。
  • 在VS2022中的容量變化:有效數(shù)據(jù)小于16時,數(shù)據(jù)存放在_buff中,無需擴容;當有效數(shù)據(jù)大于等于16時,數(shù)據(jù)存放在_str中,先2倍擴容一次,之后1.5倍擴容。
  • 再linux中的g++編譯器中的容量變化:數(shù)據(jù)只存放在_str中,容量不足就2倍擴容,感覺相當完美!
//在VS2022下的string類
class string
{
private:
	char* _buff[16]; //有效數(shù)據(jù)小于16,存放在這里
	char* _str;      //有效數(shù)據(jù)大于等于16,存放在這里
	size_t _size;
	size_t capacity;
};
int main()
{
	string s;
	size_t sz = s.capacity();
	cout << "capacity changed: " << sz << '\n';
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
	cout << "VS2022下的string類的大?。? << sizeof(s) << endl;
	return 0;
}

int main()
{
	string s1("hello xzy how are you");
	cout << s1.length() << endl;   //21
	cout << s1.size() << endl;     //21
	cout << s1.max_size() << endl; //2147483647
	cout << s1.capacity() << endl; //31
	string s2("hello xzy how are you");
	s2.reserve(100);//擴容:不一定擴100,但一定大于等于n,取決于編譯器,Linux中的g++編譯器擴的是100
	cout << s2.empty() << endl;    //0
	cout << s2.capacity() << endl; //111
	s2.shrink_to_fit();            //縮容
	cout << s2.capacity() << endl; //31
	string s3("hello xzy how are you");
	s3.resize(9);
	cout << s3 << endl; //hello xzy
	s3.resize(15, 'w');
	cout << s3 << endl; //hello xzywwwwww
	return 0;
}

6.string類對象的修改操作

  • void push_back (char c); 在字符串后尾插字符c。
  • void pop_back(); 在字符串尾刪一個字符。
  • string& append (const string& str); 在字符串后追加一個字符串。
  • string& assign (const string& str, size_t subpos, size_t sublen);
  • 拷貝字符串:從下標為subpos開始,拷貝長度為sublen的字符串到string類對象里面。
  • string& insert (size_t pos, const string& str); 在pos位置處插入字符串到string類對象里面。(由于效率問題(移動數(shù)據(jù)),謹慎使用)。
  • string& erase (size_t pos = 0, size_t len = npos); 從pos位置開始刪除長度為npos個字符。(由于效率問題(移動數(shù)據(jù)),謹慎使用)。
  • string& replace (size_t pos, size_t len, const string& str); 從pos位置開始的長度為len的子串,替換為str。(伴隨著插入與刪除,效率低,謹慎使用)。
  • void swap (string& str); 交換字符串。
int main()
{
	string s1("hello xzy");
	s1.push_back('!');
	cout << s1 << endl; //hello xzy!
	s1.pop_back();
	cout << s1 << endl; //hello xzy
	s1.append(" how are you");
	cout << s1 << endl; //hello xzy! how are you
	//可以用+=取代尾插
	s1 += "???";
	cout << s1 << endl; //hello xzy! how are you???
	string s2("hello xzy!!!");
	string s3;
	s3.assign(s2, 6, 3);
	cout << s3 << endl; //xzy
	string s4("hello xzy");
	s4.insert(0, "hello wj ");
	cout << s4 << endl; //hello wj hello xzy
	string s5("hello xzy!!!");
	s5.erase(9, 2);
	cout << s5 << endl; //hello xzy!
	string s6("hello xzy!!!");
	s6.replace(6, 5, "wj");
	cout << s6 << endl; //hello wj!
	string s7("hello x hello x");
	string tmp;
	tmp.reserve(s7.size());
	for (auto ch : s7)
	{
		if (ch == 'x')
			tmp += "wj";
		else
			tmp += ch;
	}
	cout << tmp << endl; //hello wj hello wj
	s7.swap(tmp);
	cout << s7 << endl;  //hello wj hello wj
	return 0;
}

const char* c_str() const; 返回C格式字符串。方便調用C中的接口。

int main()
{
	string file;
	cin >> file;
	//c_str()函數(shù)用于調用C語言的函數(shù),若直接傳string,類型不符合(類和指針)
	FILE* fout = fopen(file.c_str(), "r");
	if (fout == NULL)
	{
		perror("open file fail!");
		exit(1);
	}
	char ch = fgetc(fout);
	while (ch != EOF)
	{
		cout << ch;
		ch = fgetc(fout);
	}
	fclose(fout);
	fout = NULL;
	return 0;
}

7.string類對象的查找操作

  • string substr (size_t pos = 0, size_t len = npos) const; 找子串:返回從pos位置開始,長度為npos的string類。
  • size_t find (char c, size_t pos = 0) const; 從字符串pos位置開始往后找字符c,返回該字符在字符串中的位置。
  • size_t rfind (char c, size_t pos = npos) const; 從字符串pos位置開始往前找字符c,返回該字符在字符串中的位置。
  • size_t find_first_of (const char* s, size_t pos = 0) const; 從字符串pos位置開始從前往后找字符串s中出現(xiàn)的字符,返回該字符在字符串中的位置。
  • size_t find_last_of (const char* s, size_t pos = npos) const; 從字符串pos位置開始從后往前找字符串s中出現(xiàn)的字符,返回該字符在字符串中的位置。
  • size_t find_first_not_of (const char* s, size_t pos = 0) const; 從字符串pos位置開始從前往后找字符串s中沒有出現(xiàn)的字符,返回該字符在字符串中的位置。
  • size_t find_last_not_of (const char* s, size_t pos = npos) const; 從字符串pos位置開始從后往前找字符串s中沒有出現(xiàn)的字符,返回該字符在字符串中的位置。
int main()
{
	//suffix:后綴
	string s1("test.cpp");
	size_t pos1 = s1.find(".");
	string suffix1 = s1.substr(pos1);
	cout << suffix1 << endl; //.cpp
	string s2("test.cpp.zip");
	size_t pos2 = s2.rfind(".");
	string suffix2 = s2.substr(pos2);
	cout << suffix2 << endl; //.zip
	string s3("hello xzy");
	size_t found = s3.find_first_of("xzy");
	while (found != string::npos)
	{
		s3[found] = '*';
		found = s3.find_first_of("xzy", found + 1);
	}
	cout << s3 << endl; //hello ***
	string str1("/user/bin/man");
	cout << endl << str1 << "的路徑名與文件名如下:" << endl;
	size_t found1 = str1.find_last_of("/\\");
	cout << "path:" << str1.substr(0, found1) << endl;
	cout << "file:" << str1.substr(found1 + 1) << endl;
	string str2("c:\\windows\\winhelp.exe");
	cout << endl << str2 << "的路徑名與文件名如下:" << endl;
	size_t found2 = str2.find_last_of("/\\");
	cout << "path:" << str2.substr(0, found2) << endl;
	cout << "file:" << str2.substr(found2 + 1) << endl;
	return 0;
}

8.string類對象的遍歷操作

1.下標 + []

int main()
{
	string s1("hello xzy");
	//1.下標+[]
	for (int i = 0; i < s1.size(); i++)
	{
		s1[i] += 2;//可以修改
		cout << s1[i] << " ";
	}
	cout << endl << s1 << endl;
	return 0;
}

2.迭代器

int main()
{
	string s1("hello xzy");
	//2.迭代器
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		*it += 2;//可以修改
		cout << *it << " ";
		++it;
	}
	cout << endl << s1 << endl;
	return 0;
}

3.auto和范圍for

int main()
{
	string s1("hello xzy");
	//3.范圍for:字符賦值,自動迭代,自動判斷結束
	//  底層就是迭代器
	for (auto ch : s1)
	{
		ch += 2;//修改ch對于s1無影響,ch是它的拷貝
		cout << ch << ' ';
	}
	cout << endl << s1 << endl;
	return 0;
}

int main()
{
	string s1("hello xzy");
	for (auto& ch : s1)//引用:取別名,就可以修改s1
	{
		ch += 2;
		cout << ch << ' ';
	}
	cout << endl << s1 << endl;
	return 0;
}

1.auto關鍵字

  • 在早期C/C++中auto的含義是:使用auto修飾的變量,是具有自動存儲器的局部變量,后來這個不重要了。C++11中,標準委員會變廢為寶賦予了auto全新的含義即:auto不再是一個存儲類型指示符,而是作為一個新的類型指示符來指示編譯器,auto聲明的變量必須由編譯器在編譯時期 推導而得
  • 用auto聲明指針類型時,用auto和auto*沒有任何區(qū)別,但用auto聲明引用類型時則必須加&。
  • 當在同一行聲明多個變量時,這些變量必須是相同的類型,否則編譯器將會報錯,因為編譯器實際只對第一個類型進行推導,然后用推導出來的類型定義其他變量。
  • auto不能作為函數(shù)的參數(shù),可以做返回值,但是建議謹慎使用。
  • auto不能直接用來聲明數(shù)組。
#include<map>
//auto不能做參數(shù)
//void func1(auto a) error
//{}
//auto可以做返回值,但是建議謹慎使用
auto func2()
{
	return 2;
}
int main()
{
	int a = 10;
	auto b = a;//編譯期間自動推導類型
	auto c = 'a';
	auto d = func2();
	//auto e; auto必須初始化,否則不知道開多少空間
	int x = 10;
	auto y = &x;
	auto* z = &x;//可以不寫*
	auto& m = x;//必須加上&
	auto aa = 1, bb = 2;//right
	//auto cc = 3, dd = 4.0; error必須始終推導為同一類型
	//auto array[] = { 4, 5, 6 }; error數(shù)組不能具有其中包含“auto”的元素類型
	// auto 的價值
	map<string, string> dict; //初始化二叉樹
	//map<string, string>::iterator mit = dict.begin();
	auto mit = dict.begin();
	cout << typeid(mit).name() << endl;
	return 0;
}

2.范圍for

  • 對于一個有范圍的集合而言,由程序員來說明循環(huán)的范圍是多余的,有時候還會容易犯錯誤。因此C++11中引入了基于范圍的for循環(huán)。for循環(huán)后的括號由冒號“ :”分為兩部分:第一部分是范圍內用于迭代的變量,第二部分則表示被迭代的范圍,自動迭代,自動取數(shù)據(jù),自動判斷結束。
  • 范圍for可以作用到數(shù)組和容器對象上進行遍歷。
  • 范圍for的底層很簡單,容器遍歷實際就是替換為迭代器,這個從匯編層也可以看到。
int main()
{
	int array[] = { 1, 2, 3, 4, 5 };
	//范圍for適用于《容器》和《數(shù)組》
	// C++98的遍歷
	for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++)
	{
		cout << array[i] << ' ';
	}
	cout << endl;
	// C++11的遍歷
	for (auto& i : array)
	{
		cout << i << ' ';
	}
	cout << endl;
	return 0;
}

四.非成員函數(shù):

  • getline() istream& getline (istream& is, string& str, char delim);
  • delim:分隔符istream& getline (istream& is, string& str);

類似C語言中的scanf(“%s”, str),但是其遇到空格會停止;
C++中引入了getline優(yōu)化了scanf遇到的問題,默認遇到\n才停止,也可以自定義停止字符delim。

例題:字符串最后一個單詞的長度

#include <iostream>
#include<string>
using namespace std;
int main() 
{
    string str;
    getline(cin, str);
    size_t pos = str.rfind(' ');
    string sub = str.substr(pos + 1);
    cout << sub.size() << endl;
}

五.string——>OJ題

 ?把字符串轉換成整數(shù)(atoi)

字符串相加

反轉字符串

字符串最后一個單詞的長度

驗證回文串

反轉字符串||

字符串中的第一個唯一字符

反轉字符串中的單詞|||

字符串相乘

到此這篇關于C++標準庫 介紹及使用string類的文章就介紹到這了,更多相關C++使用string類內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論