1. 程式人生 > >模板類——string類方法總結

模板類——string類方法總結

string類

string類是C++標準庫的一個重要的部分,也是字串的一種資料型別,相對於char*字串它更方便強大,我們不必擔心記憶體是否足夠、字串長度等等,而且作為一個泛型類出現,他整合的操作函式足以完成我們大多數情況下的需要。我們可以用
= 進行賦值操作,== 進行比較,+ 做串聯等

基本賦值方法

有以下賦值方式:

    string name("Adam");
	string a, b, c, d,e;
	a = name;
	b = "Road changan";
	c = 'X';
	d = name + b;
	e = { 'h','e'
,'l','l','o' };

字串簡單資料方法

begin() 指向字串第一個字元的迭代器
end() 超尾值的迭代器,實際指向最後一個元素之後的位置
rbegin() 超尾值的反轉迭代器
rend() 指向第一個字元的翻轉迭代器
size() 字串中的元素數
length() 和size作用相同
capacity 給字串分配的元素數 ,,capacity-size即為字串還可以附加多少個字元就需分配更多記憶體
max_size() 字串型別的最大長度
data() 返回字串的值
c_str() 返回字串的值
	string str1 = "hello world";
   cout <<"begin:  "<< *str1.begin() << endl;
	cout <<"end:  "<<
*(str1.end()-1) << endl; cout <<"rbegin: " << *str1.rbegin() << endl; cout << "rend: " << *(str1.rend() - 1) << endl; cout << "size: " << str1.size() << endl; cout << "length: " << str1.length() << endl; cout << "capacity: " << str1.capacity() << endl; cout << "max_size: " << str1.max_size() << endl; cout << "data: " << str1.data() << endl; cout << "c_str: " << str1.c_str() << endl;

結果:

begin: h
end: d
rbegin: d
rend: h
size: 11
length: 11
capacity: 15
max_size: 2147483647
data: hello world
c_str: hello world

字串記憶體方法

resize(n,c) 如果n>npos,則異常out of range,n>size,則用c來補充字串,n<size,則階段字串
reserve(n) 將字串設定為n大小,以前的指標,引用,迭代器無效
shrink_to_fit() 將capacity的值與size相同,回收記憶體
clear() 清空字串
empty() 判斷字串是否為空
    string str1 = "hello world";
	str1.resize(15, 'b');
	cout << "str1:  " << str1 << endl;
	str1.reserve(30);
	cout << "str1 capacity:" << str1.capacity() << endl;
	str1.shrink_to_fit();
	cout << "str1 size: " << str1.size() << endl;
	cout << "shrink_to_fit" << str1.capacity()<< endl;
	str1.clear();
	cout << "empty:  " << str1.empty() << endl;

結果:

str1: hello worldbbbb
str1 capacity:31
str1 size: 15
shrink_to_fit15
empty: 1

字串存取

  • at方法
  • []方法
  • front方法
  • back方法
	string str1 = "hello world";
	str1[0] = 'r';
	str1.at(1) = 'a';
	cout << "str1:" << str1 << endl;
	cout << "str1[10]:" << str1[10] << endl;
	cout << "str1[10]:" << str1.at(10) << endl;
	cout << "front: " << str1.front() <<endl;
	cout << "back: " << str1.back() << endl;

str1:rallo world
str1[10]:d
str1[10]:d
front: r
back: d

字串搜尋

  • find() 返回字串或者字元首字元的位置
  • rfind() 搜尋字串最後一次出現的位置
  • find_first_of() 返回子字串中的字元首次出現的位置
  • find_last_of() 返回字串中的某一個字元最後出現的位置
  • find_first_not_of() 返回第一個不位於子字串的第一個字元的位置
  • find_last_not_of() 返回第一個不位於子字串的最後一個字元的位置
	string str1 = "hello world";
	cout << "find:" << str1.find("h",5) << endl;
	cout << "find:" << str1.find("h") << endl;
	cout << "rfind:" << str1.rfind("l") << endl;;
	cout << "find_last_of:" << str1.find_last_of("abcde") << endl;
	cout << "find_first_of:" << str1.find_first_of("abcde") << endl;
	cout << "find_last_not_of:" << str1.find_last_not_of("abcde") << endl;
	cout << "find_first_not_of:" << str1.find_first_not_of("abcde") << endl;

find:4294967295 //從第五個位置開始,未找到h
find:0
rfind:9
find_last_of:10
find_first_of:1
find_last_not_of:9
find_first_not_of:0

其他字串方法

  • compare() 兩個字串比較,或者字串的子串比較
  • append() 將一個字串或單個字串追加到另一個字串物件後面
  • assign() 將整個字串,字串的一部分或由相同字串組成的字元序列賦值給字串序列
  • insert() 將string物件或者字串陣列或者幾個字元插入到string物件中
  • erase() 從字串中刪除字元
  • replace() 指定了要替換的字串部分和用於替換的內容
  • copy() 將string物件或者其中一部分複製到指定的字串陣列中
  • swap() 用一個時間恆定的演算法來交換兩個string物件的內容

—————————————————————————————
compare方法: 當s1在s2前面時返回-1,相同返回0,在後面返回1

	string s1("bellflower");
	string s2("bell");
	string s3("cat");
	string s4("apple");
	int a1 = s1.compare(s3);
	int a2 = s1.compare(0,4,s2,0,4);
	int a3 = s1.compare(s4);
	cout << "a1: " << a1 << endl;//返回 -1
	cout << "a2: " << a2 << endl;//返回 0
	cout << "a3: " << a3 << endl;//返回 1

append方法: 對字串進行追加

	string s1("bellflower");
	string s2("big");
	s2.append(s1);
	cout << "s2:" << s2 << endl;
	s2.append("abcde");
	cout << "s2:" << s2 << endl;
	cout<<"s2:"<<s1+s2<<endl;

> 輸出:
> s2:bigbellflower
s2:bigbellflowerabcde
s2:bellflowerbigbellflowerabcde

assign方法: 對一個字串進行賦值

	string str1;
	string str2("helloworld");
	str1.assign(str2,0,5);
	cout << "str1:" << str1 << endl;
	str1.assign(str2);
	cout << "str1:" << str1 << endl;
	str1.assign(8, '&');
	cout << "str1:" << str1 << endl;

> 輸出:
> str1:hello
str1:helloworld
str1:&&&&&&&&

insert方法: 在字串的某一個位置插入字串

	string str2("helloworld");
	str2.insert(0, "big");
	cout << "str2:" << str2 << endl;
	str2.insert(str2.size()," girl like it");

> 輸出:
> str2:bighelloworld
str2:bighelloworld girl like it

erase方法: 刪除位置n到字串尾的字元
pop_back() 刪除字串的最後一個字元

	string str2("helloworld");
	str2.erase(6);
	cout << "str2:" << str2 << endl;

> 輸出:
> hellow

replace方法 制定要替換的字串部分和用於替換的內容
開始替換的位置和長度,和要替換的字串

	string str2("helloworld");
	str2.replace(5,5, "china");
	cout << "str2:" << str2 << endl;
	

> 輸出:
> str2:hellochina

swap()方法: 交換兩個字串的內容

	string str2("helloworld");
	string str1("china");
	swap(str1,str2);
	cout << "str2:" << str2 << endl;

> str2:china