1.簡介:
string是C++標準庫的一個重要的部分,主要用於字符串處理,在使用時,務必添加頭文件 。
2.string常用函數:
(1)構造函數:
-
生成一個空字符串str:
string str;
-
拷貝構造函數,生成str的複製品:
string s(str);
-
將字符串str中從下標strbegin開始、長度爲strlen的部分作爲字符串初值:
string s(str, strbegin,strlen) -
生成num個c字符的字符串:
string s(num ,c)
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1;
string str2("123456789");
string str3("123456", 0, 3);
string str4("0123456", 5);
string str5(5, '1');
cout<<str1<<endl;
cout<<str2<<endl;
cout<<str3<<endl;
cout<<str4<<endl;
cout<<str5<<endl;
}
(2)string的大小和容量:
-
返回string對象的字符個數,下面兩個的執行效果相同:
size(); length();
-
返回string對象最多包含的字符數,超出會拋出length_error異常:
max_size();
-
重新分配內存之前,string對象能包含的最大字符數:
capacity();
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1="Hello World";
string str2="123456";
int len1=str1.length();
int len2=str2.size();
int len3=str1.max_size();
int len4=str1.capacity();
cout<<len1<<endl;
cout<<len2<<endl;
cout<<len2<<endl;
cout<<len4<<endl;
}
(3)轉換爲char*
-
使用c_str();方法:
c_str();
-
cout 可直接輸出 string 類的對象的內容;
-
使用 c_str() 方法轉換 string 類型到 char* 類型時,需要爲char*添加 const 關鍵字;
-
printf() 函數不能直接打印 string 類的對象的內容,可以通過將 string 轉換爲 char* 類型,再使用 printf() 函數打印。
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1="Hello World";
// string 轉換爲 char*
const char* str2 = str1.c_str();
cout<<str1<<endl;
cout<<str2<<endl;
}
(4)判斷是否爲空:
== empty() ==
if (str1.empty())
{
cout << "str1 is empty." << endl;
}
(5)char*、char[]轉換爲string
實際上是將 char 、char[] 定義的字符串的首地址賦值給 string 對象
#include <string>
#include <iostream>
using namespace std;
int main()
{
const char* psz1 = "liitdar";
char psz2[] = "alliance";
string str1;
string str2;
str1 = psz1;
str2 = psz2;
cout<< str1 << endl;
cout<< str2 << endl;
}
(6)string的插入:
push_back() 和 insert()
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str1;
// 尾插一個字符
str1.push_back('a');
str1.push_back('b');
str1.push_back('c');
cout<<str1<<endl;
// insert(pos,char):在制定的位置pos前插入字符char
str1.insert(str1.begin(),'1');
cout<<str1<<endl;
}
(7)string拼接字符串:
append() & + 操作符
#include <string>
#include <iostream>
using namespace std;
int main()
{
// 方法一:append()
string s1("abc");
s1.append("def");
cout<<s1<<endl; // s1:abcdef
// 方法二:+ 操作符
string s2 = "abc";
/*s2 += "def";*/
string s3 = "def";
s2 += s3.c_str();
cout<<"s2:"<<s2<<endl; // s2:abcdef
}
(8)string的大小寫轉換:
transform算法,配合的toupper和tolower
transform(coll.begin(), coll.end(), coll.begin(), fun);
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s = "ABCDEFG";
transform(s.begin(),s.end(),s.begin(),::tolower);
cout<<s<<endl;
return 0;
}
(9)string的查找:find
- size_t find (constchar* s, size_t pos = 0) const;
` //在當前字符串的pos索引位置開始,查找子串s,返回找到的位置索引,
-1表示查找不到子串
- size_t find (charc, size_t pos = 0) const;
//在當前字符串的pos索引位置開始,查找字符c,返回找到的位置索引,
-1表示查找不到字符
- size_t rfind (constchar* s, size_t pos = npos) const;
//在當前字符串的pos索引位置開始,反向查找子串s,返回找到的位置索引,
-1表示查找不到子串
- size_t rfind (charc, size_t pos = npos) const;
//在當前字符串的pos索引位置開始,反向查找字符c,返回找到的位置索引,-1表示查找不到字符
- size_tfind_first_of (const char* s, size_t pos = 0) const;
//在當前字符串的pos索引位置開始,查找子串s的字符,返回找到的位置索引,-1表示查找不到字符
- size_tfind_first_not_of (const char* s, size_t pos = 0) const;
//在當前字符串的pos索引位置開始,查找第一個不位於子串s的字符,返回找到的位置索引,-1表示查找不到字符
- size_t find_last_of(const char* s, size_t pos = npos) const;
//在當前字符串的pos索引位置開始,查找最後一個位於子串s的字符,返回找到的位置索引,-1表示查找不到字符
- size_tfind_last_not_of (const char* s, size_t pos = npos) const;
//在當前字符串的pos索引位置開始,查找最後一個不位於子串s的字符,返回找到的位置索引,-1表示查找不到子串
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s("dog bird chicken bird cat");
//字符串查找-----找到後返回首字母在字符串中的下標
// 1. 查找一個字符串
cout << s.find("chicken") << endl; // 結果是:9
// 2. 從下標爲6開始找字符'i',返回找到的第一個i的下標
cout << s.find('i',6) << endl; // 結果是:11
// 3. 從字符串的末尾開始查找字符串,返回的還是首字母在字符串中的下標
cout << s.rfind("chicken") << endl; // 結果是:9
// 4. 從字符串的末尾開始查找字符
cout << s.rfind('i') << endl; // 結果是:18-------因爲是從末尾開始查找,所以返回第一次找到的字符
// 5. 在該字符串中查找第一個屬於字符串s的字符
cout << s.find_first_of("13br98") << endl; // 結果是:4---b
// 6. 在該字符串中查找第一個不屬於字符串s的字符------先匹配dog,然後bird匹配不到,所以打印4
cout << s.find_first_not_of("hello dog 2006") << endl; // 結果是:4
cout << s.find_first_not_of("dog bird 2006") << endl; // 結果是:9
// 7. 在該字符串最後中查找第一個屬於字符串s的字符
cout << s.find_last_of("13r98") << endl; // 結果是:19
// 8. 在該字符串最後中查找第一個不屬於字符串s的字符------先匹配t--a---c,然後空格匹配不到,所以打印21
cout << s.find_last_not_of("teac") << endl; // 結果是:21
}
#include <string>
#include <iostream>
using namespace std;
int main()
{
// 待檢索的字符串
string strOutput = "|0|1|2|";
// 需要檢索的子串
string strObj = "|1|";
// 子串位於字符串中的位置
size_t nLoc = strOutput.find(strObj);
// 如果檢索到子串在字符串中,則打印子串的位置
if (nLoc != string::npos)
{
cout << "nLoc is: " << nLoc << endl;
}
}
(10)string排序:
sort(s.begin(),s.end())
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
string s = "cdefba";
sort(s.begin(),s.end());
cout<<s<<endl;
}