文章目錄
-
- 引言
- 2.2.1 string的特性
- 2.2.2 string用法理論
-
- 2.2.2.1 string構造函數
- 2.2.2.2 string指派操作
- 2.2.2.3 string取值操作
- 2.2.2.4 string拼接操作
- 2.2.2.6 string查找
- 2.2.2.7 string替換
- 2.2.2.7 string字元串比較
- 2.2.2.8 string字元串插入和删除
- 2.2.2.9 string子串
- string用法示例代碼
- string用法示例代碼結果
引言
string容器
string是C++風格的字元串,而string本質上是一個類
C++ STL中的string差別于C語言風格的cstring, 對于這一點有疑惑的小夥伴推薦先看一下這篇文章:
C++筆記(cstring和string的差別)
2.2.1 string的特性
- Char為指針,String是一個類。string封裝了char,管理這個字元串,是一個char*型的容器。
- 不用考慮記憶體釋放和越界
-
String封裝了很多實用的成員方法
查找,拷貝,删除…
2.2.2 string用法理論
2.2.2.1 string構造函數
string(); //創造一個空的字元串
string(const char*s) //使用字元串s初始化
string(const string& str); //使用一個string對象初始化另一個string對象
string(int n, char c); //使用n個字元c初始化
2.2.2.2 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賦給目前字元串
2.2.2.3 string取值操作
這裡參考下面代碼區text03函數
string s = "abcdefg";
//重載[]操作符
for (int i = 0;i < s.size(); i++)
{
cout << s[i] << " ";
}
cout << endl;
//at成員函數
for (int i = 0; i < s.size(); i++)
{
cout << s.at(i) << " ";
}
cout << endl;
2.2.2.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個字元連接配接到字元串結尾
2.2.2.6 string查找
//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最後一次出現位置
2.2.2.7 string替換
//string替換
string& replace(int pos, int n, const string& str); //替換從pos開始n個字元為字元串str
string& replace(int pos, int n, const char* s); //替換從pos開始的n個字元為字元串s
2.2.2.7 string字元串比較
字元串比較是按字元的ASCII碼進行對比或者說是按照字典序,字母越靠前越小。
注意:大寫A比小寫a小
(等于) = 傳回 0
(大于) > 傳回 1
(小于) < 傳回 -1
int compare(const string &s) const; // 與字元串s比較
int compare(const char *s) const; //與字元串s比較
2.2.2.8 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個字元
2.2.2.9 string子串
string用法示例代碼
//函數text0~9對應2.2.2.1~2.2.2.9
#include<iostream>
#include<string>
using namespace std;
//初始化
void text01()
{
cout << "\ntext01\n";
string s1;//調用無參構造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3);//拷貝構造
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
}
//指派操作
void text02()
{
cout << "\ntext02\n";
string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
//成員方法
s1.assign("jkl");
cout << s1 << endl;
}
//取值操作
void text03()
{
cout << "\ntext03\n";
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;
}
}
//拼接操作
void text04()
{
cout << "\ntext04\n";
//1
string s = "abcd";
s += "abcd";
string s2 = "1111";
s += s2;
cout << s << endl;
//2
string s3 = "2222";
s2.append(s3);
cout << s2 << endl;
string s4 = s2 + s3;
cout << s4 << endl;;
}
//查找操作
void text05()
{
cout << "\ntext05\n";
string s = "abcdefghjkl";
//查找第一次出現的位置
int pos = s.find("fg");
cout << "pos:" << pos << endl;//傳回5
//查找最後一次出現的位置
pos = s.rfind("fg");
cout << "pos:" << pos << endl;
}
//替換操作
void text06()
{
cout << "\ntext06\n";
string s = "abcdefg";
s.replace(1,3,"111");
cout << s << endl;
}
//字元串比較
void text07()
{
cout << "\ntext07\n";
string s1 = "abcd", s2 = "abce";
if (s1.compare(s2) == 0)
{
cout << "相等" << endl;
}
else
{
cout << "不相等 值為:" << s1.compare(s2) << endl;
//ASCII碼字典序,小于為-1。
}
}
//string子串
void text08()
{
cout << "\ntext08\n";
string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
}
//string插入和删除操作
void text09()
{
cout << "\ntext09\n";
string s = "abcdefg";
s.insert(3, "111");
cout << s << endl;
s.erase(0, 2);//從0位置開始删2個元素
cout << s << endl;
}
int main()
{
text01();
text02();
text03();
text04();
text05();
text06();
text07();
text08();
text09();
return 0;
}
string用法示例代碼結果
謝謝閱讀(〃’ ▽ '〃)如有纰漏歡迎指出,覺得還不錯就點個贊吧。