中國大學MOOC程序設計與算法(三):C++ 面向對象程序設計 第八週 標準模板庫STL(一) 筆記 之 string類

第八週 標準模板庫STL(一)
1.string類
2.標準模板庫STL概述(一)
3.標準模板庫STL概述(二)
4.vector,deque和list
5.函數對象

1.string類

string 類

string 類是模板類:typedef basic_string string;
使用string類要包含頭文件

string對象的初始化:

string s1("Hello");
string month = "March";
string s2(8,'x');//s2 = "xxxxxxxx";

錯誤的初始化方法:

string error1 =  'c'; // 錯
string error2('u'); // 錯
string error3 = 22; // 錯
string error4(8); // 錯

可以將字符賦值給string對象

string s;
s = 'n';

例子:

#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[ ]){
	string s1("Hello");
	cout << s1 << endl;
	string s2(8,'x');
	cout << s2 << endl;
	string month = "March";
	cout << month << endl;
	string s;
	s='n';
	cout << s << endl;
	return 0;
}
輸出:
Hello
xxxxxxxx
March
n

string 對象的長度用成員函數 length()讀取;

string s("hello");
cout << s.length() << endl;

string 支持流讀取運算符

string stringObject;
cin >> stringObject;

string 支持getline函數

string s;
getline(cin ,s);

string 的賦值和連接

用 = 賦值

string s1("cat"), s2;
s2 = s1;//s1,s2不相關

用 assign 成員函數複製

string s1("cat"), s3;
s3.assign(s1);

用 assign 成員函數部分複製

string s1("catpig"), s3;
s3.assign(s1, 1, 3);//從s1 中下標爲1的字符開始複製3個字符給s3

單個字符複製

s2[5] = s1[3] = ‘a’;

逐個訪問string對象中的字符

string s1("Hello");
for(int i=0;i<s1.length();i++)
cout << s1.at(i) << endl;
//後者 cout << s1[i] << endl;

訪問string對象中的字符有兩種方式,s[i]和s.at(i),區別:成員函數at會做範圍檢查,如果超出範圍,會拋出out_of_range異常,而下標運算符[]不做範圍檢查。

用 + 運算符連接字符串

string s1("good "), s2("morning!");
s1 += s2;
cout << s1; //good morning!

用成員函數 append 連接字符串

string s1("good "), s2("morning! ");
s1.append(s2);//s1 = "good morning!"
cout << s1;
s2.append(s1, 3, s1.size());//s1.size(),s1字符數
cout << s2;
// 下標爲3開始,s1.size()個字符,如果字符串內沒有足夠字符,則複製到字符串最後一個字符

比較string

用關係運算符比較string的大小
– == , >, >=, <, <=, !=
返回值都是bool類型,成立返回true, 否則返回false
例如:

string s1("hello"),s2("hello"),s3("hell");
bool b = (s1 == s2);
cout << b << endl;
b = (s1 == s3);
cout << b << endl;
b = (s1 > s3);
cout << b << endl;
輸出:
1
0
1

用成員函數compare比較string的大小

string s1("hello"),s2("hello"),s3("hell");
int f1 = s1.compare(s2);
int f2 = s1.compare(s3);
int f3 = s3.compare(s1);
int f4 = s1.compare(1,2,s3,0,3); //s1[1-2]; s3[0-3]
int f5 = s1.compare(0,s1.size(),s3);//s1 0-end
cout << f1 << endl << f2 << endl << f3 << endl;
cout << f4 << endl << f5 << endl;
輸出:
0 // hello == hello
1 // hello > hell
-1 // hell < hello
-1 // el < hell
1 // hello > hell

子串

成員函數 substr(i,length):下標i開始的長度爲5的子串,若越過結尾,就只到結尾。

string s1("hello world"), s2;
s2 = s1.substr(4,5); // 下標4開始5個字符
cout << s2 << endl;
輸出:
o wor

交換string

成員函數 swap

string s1("hello world"), s2("really");
s1.swap(s2);
cout << s1 << endl;
cout << s2 << endl;
輸出:
really
hello world

尋找string中的字符

成員函數 find()
在s1中從前向後查找 “lo” 第一次出現的地方,如果找到,返回 “lo”開始的位置,即 l 所在的位置下標。如果找不到,返回string::npos (string中定義的靜態常量)。

string s1("hello world");
s1.find("lo");

成員函數 rfind()
在s1中從後向前查找 “lo” 第一次出現的地方,如果找到,返回 “lo”開始的位置,即 l 所在的位置下標,也是相對於字符串開頭的下標。如果找不到,返回string::npos 。

string s1("hello world");
s1.rfind("lo");

find()可以指定開始查找的位置,但是返回值還是相對於開頭的下標。

string s1("hello worlld");
cout << s1.find("ll",1) << endl;
cout << s1.find("ll",2) << endl;
cout << s1.find("ll",3) << endl;
// 分別從下標1,2,3開始查找“ll”
輸出:
2
2
9

成員函數 find_first_of()
在s1中從前向後查找 “abcd” 中任何一個字符第一次出現的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。

string s1("hello world");
s1.find_first_of(“abcd");

成員函數 find_last_of()
在s1中查找 “abcd” 中任何一個字符最後一次出現的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。

string s1("hello world");
s1.find_last_of(“abcd");

成員函數 find_first_not_of()
在s1中從前向後查找不在 “abcd” 中的字母第一次出現的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。

string s1("hello world");
s1.find_first_not_of(“abcd");//找到的是開頭'h'的下標

成員函數 find_last_not_of()
在s1中從後向前查找不在 “abcd” 中的字母第一次出現的地方,如果找到,返回找到字母的位置,如果找不到,返回string::npos。

string s1("hello world");
s1.find_last_not_of(“abcd");
string s1("hello worlld");
cout << s1.find("ll") << endl;
cout << s1.find("abc") << endl;
cout << s1.rfind("ll") << endl;
cout << s1.rfind("abc") << endl;
cout << s1.find_first_of("abcde") << endl;
cout << s1.find_first_of("abc") << endl;
cout << s1.find_last_of("abcde") << endl;
cout << s1.find_last_of("abc") << endl;
cout << s1.find_first_not_of("abcde") << endl;
cout << s1.find_first_not_of("hello world") << endl;
cout << s1.find_last_not_of("abcde") << endl;
cout << s1.find_last_not_of("hello world") << endl;
輸出 :
2
4294967295
9
4294967295
1
4294967295
11
4294967295
0
4294967295
10
4294967295
//4294967295就是找不到返回的nops

刪除string中的字符

成員函數erase()

string s1("hello worlld");
s1.erase(5);
cout << s1;
cout << s1.length();
cout << s1.size();
// 去掉下標 5 及之後的字符
輸出:
hello55

替換string中的字符

成員函數 replace()

string s1("hello world");
s1.replace(2,3, “haha");
cout << s1;
//將s1中下標2 開始的3個字符換成“haha”
輸出:hehaha world
string s1("hello world");
s1.replace(2,3, "haha", 1,2);
cout << s1;
// 將s1中下標2 開始的3個字符換成“haha” 中下標1開始的2個字符
輸出:heah world

在string中插入字符

成員函數insert()

string s1("hello world");
string s2(“show insert");
s1.insert(5,s2); // 將s2插入s1下標5的位置
cout << s1 << endl;
s1.insert(2,s2,5,3);
//將s2中下標5開始的3個字符插入s1下標2的位置
cout << s1 << endl;
輸出:
helloshow insert world
heinslloshow insert world

轉換成C語言式char *字符串

成員函數 c_str()

string s1("hello world");
printf("%s\n", s1.c_str());
// s1.c_str() 返回傳統的const char * 類型字符串,且該字符串以‘\0’結尾。
輸出:
hello world

成員函數data()

string s1("hello world");
const char * p1=s1.data();
for(int i=0;i<s1.length();i++)
	printf("%c",*(p1+i));
//s1.data() 返回一個char * 類型的字符串,對s1 的修改可能會使p1出錯。
輸出:
hello world

字符串拷貝

成員函數copy()

string s1("hello world");
int len = s1.length();
char * p2 = new char[len+1];
s1.copy(p2,5,0);
p2[5]=0;
cout << p2 << endl;
// s1.copy(p2,5,0) 從s1的下標0的字符開始製作一個最長5個字符長度的字符串副本並將其賦值給p2。返回值表明實際複製字符串的長度。
輸出:
hello

字符串流處理

除了標準流和文件流輸入輸出外,還可以從string進行輸入輸出;
類似 istream和osteram進行標準流輸入輸出,我們用istringstream 和 ostringstream進行字符串上的輸入
輸出,也稱爲內存輸入輸出。

#include <string>
#include <iostream>
#include <sstream>

字符串輸入流 istringstream

string input("Input test 123 4.7 A");
istringstream inputString(input);
string string1, string2;
int i;
double d;
char c;
inputString >> string1 >> string2 >> i >> d >> c;
cout << string1 << endl << string2 << endl;
cout << i << endl << d << endl << c <<endl;
long L;
if(inputString >> L) cout << "long\n";//inputString已經讀完了,爲空
else cout << "empty\n";
輸出:
Input
test
123
4.7
A
empty

字符串輸出流 istringstream

ostringstream outputString;
int a = 10;
outputString << "This " << a << "ok" << endl;
cout << outputString.str();
輸出:
This 10ok
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章