閱讀614 返回首頁    go 阿裏雲 go 技術社區[雲棲]


STL之一:字符串用法詳解

    字符串是程序設計中最複雜的變成內容之一。STL string類提供了強大的功能,使得許多繁瑣的編程內容用簡單的語句就可完成。string字符串類減少了C語言編程中三種最常見且最具破壞性的錯誤:超越數組邊界;通過違背初始化或被賦以錯誤值的指針來訪問數組元素;以及在釋放了某一數組原先所分配的存儲單元後仍保留了“懸掛”指針。

    string類的函數主要有:

    

Member functions
(constructor)
Construct string object (public member function )
(destructor)
String destructor (public member function )
operator=
String assignment (public member function )

Iterators:
begin
Return iterator to beginning (public member function )
end
Return iterator to end (public member function )
rbegin
Return reverse iterator to reverse beginning (public member function )
rend
Return reverse iterator to reverse end (public member function )
cbegin 
Return const_iterator to beginning (public member function )
cend 
Return const_iterator to end (public member function )
crbegin 
Return const_reverse_iterator to reverse beginning (public member function )
crend 
Return const_reverse_iterator to reverse end (public member function )

Capacity:
size
Return length of string (public member function )
length
Return length of string (public member function )
max_size
Return maximum size of string (public member function )
resize
Resize string (public member function )
capacity
Return size of allocated storage (public member function )
reserve
Request a change in capacity (public member function )
clear
Clear string (public member function )
empty
Test if string is empty (public member function )
shrink_to_fit 
Shrink to fit (public member function )

Element access:
operator[]
Get character of string (public member function )
at
Get character in string (public member function )
back 
Access last character (public member function )
front 
Access first character (public member function )

Modifiers:

operator+=
Append to string (public member function )
append
Append to string (public member function )
push_back
Append character to string (public member function )
assign
Assign content to string (public member function )
insert
Insert into string (public member function )
erase
Erase characters from string (public member function )
replace
Replace portion of string (public member function )
swap
Swap string values (public member function )
pop_back 
Delete last character (public member function )

String operations:
c_str
Get C string equivalent (public member function )
data
Get string data (public member function )
get_allocator
Get allocator (public member function )
copy
Copy sequence of characters from string (public member function )
find
Find content in string (public member function )
rfind
Find last occurrence of content in string (public member function )
find_first_of
Find character in string (public member function )
find_last_of
Find character in string from the end (public member function )
find_first_not_of
Find absence of character in string (public member function )
find_last_not_of
Find non-matching character in string from the end (public member function )
substr
Generate substring (public member function )
compare
Compare strings (public member function )

Member constants
npos
Maximum value for size_t (public static member constant )

Non-member functions overloads
operator+
Concatenate strings (function )
relational operators
Relational operators for string (function )
swap
Exchanges the values of two strings (function )
operator>>
Extract string from stream (function )
operator<<
Insert string into stream (function )
getline
Get line from stream into string (function )

1.string對象的定義和初始化

    初始化string對象的幾種方式有:
  1. string s1;//默認構造函數,s1為空串
  2. string s2(s1);//將s2初始化為s1的一個副本
  3. string s3("value");//將s3初始化為value
  4. string s4(n,'c');//將s4初始化為字符'c'的n個副本
  5. string s5(s4,0,3)//從s4中下標為0的字符開始,連續取3個字符構成s5
  6. string s6 = s5 + "value";//value 接在s5後麵,注意+操作符的左右操作數至少有一個是string類型的
  7. 迭代器創建,    由於可將string看作字符的容器對象,因此可以給string類的構造函數傳遞兩個迭代器,將它們之間的數據複製到心的string對象中。
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string s1("How are you");
	string s2(s1.begin(),s1.end());
	string s3(s1.begin()+4,s1.begin()+7);
	cout<<s1<<endl;
	cout<<s2<<endl;
	cout<<s3<<endl;
	return 0;
}


2.string 對象的讀寫

    string對象的讀寫可以通過兩個方式:
  1. 通過cin從標準輸入中讀取,cin忽略開題所有的空白字符,讀取字符直至再次遇到空白字符,讀取終止。
  2. 用getline讀取整行文本,getline函數接受兩個參數:一個輸入流對象和一個string對象。getline函數從輸入流的下一行讀取,並保存讀取的內容到string中,但不包括換行符。和輸入操作符不一樣的是,getline並不忽略開頭的換行符。即便它是輸入的第一個字符,getline也將停止讀入並返回。如果第一個字符就是換行符,則string參數將被置為空string。

3.string對象的插入操作

    字符串一般通過包括首字符前、尾字符後、任意位置插入等幾種情況。
    
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string s1("do");
	cout<<"Initial size is:"<<s1.size()<<endl;
	s1.insert(0,"How ");
	s1.append(" you");
	s1 = s1 + " do ?";
	cout<<"Final size is:"<<s1.size()<<endl;
	cout<<s1<<endl;
	return 0;
}
程序運行結果如下:

通過該函數可以得出:
  1.     insert函數,第一個參數表明插入源串的位置,第二個參數表麵要插入的字符串,因此利用該函數可以實現串首、串尾及任意位置處的字符串插入功能。
  2. append函數,僅有一個輸入參數,在源字符串尾部追加該字符串。
  3. 利用+實現字符串的連接,從而創建新的字符串。

4.替換操作

    常用的是replace函數,有三個輸入參數:第一個用於指示從字符串的什麼位置開始改寫,第二個用於指示從源字符串中刪除多少個字符,第三個是替換字符串的值。
    
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string s1("I love you forever !");
	cout<<"替換前:"<<s1<<endl;
	s1.replace(7,3,"dachun");
	cout<<"替換後"<<s1<<endl;
	
	return 0;
}
程序運行結果如下:


5.查詢操作

    查詢常用的函數有:
  1.     string::npos:這是string類中的一個成員變量,一般應用在判斷係統查詢函數的返回值上,若等於該值,表明沒有符合查詢條件的結果值。
  2. find函數:在一個字符串中查找指定的單個字符或字符組。如果找到,就返回首次匹配的開始位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
  3. find_first_of函數:在一個字符串中進行查找,返回值是第一個與指定字符串中任何字符匹配的字符位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
  4. find_last_of函數:在一個字符串中進行查找,返回值是最後一個與指定字符串中任何字符匹配的字符位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
  5. find_first_not_of函數:在一個字符串中進行查找,返回值是第一個與指定字符串中任何字符都不匹配的字符位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
  6. find_last_not_of函數:在一個字符串中進行查找,返回下標值最大的與指定字符串中任何字符都不匹配的字符位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
  7. rfind函數:對一個串從尾至頭查找指定的單個字符或字符組,如果找到,就返回首次匹配的開始位置;如果沒有找到匹配的內容,則返回string::npos。一般有兩個輸入參數,一個是待查詢的字符串,一個是查詢的起始位置,默認起始位置為0.
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string s1("what's your name? my name is TOM. How do you do? Fine,thanks.");
	int n = s1.find("your");
	cout<<"the first your pos:"<<n<<endl;
	n = s1.find("you",15);
	cout<<"the first you pos begin from 15:"<<n<<endl;
	n = s1.find_first_of("abcde");
	cout<<"find pos when character within abcde:"<<n<<endl;
	n = s1.find_first_of("abcde",3);
	cout<<"find pos when character within abcde from third character:"<<n<<endl;
	
	return 0;
}

程序運行結果如下:

 

6.刪除字符操作

    主要用erase函數,有兩個迭代器輸入參數,之間表示的字符將被刪除掉。
    
#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	string s1("what's your name? my name is TOM. How do you do? Fine,thanks.");
	s1.erase(s1.begin(),s1.begin()+17);
	cout<<"after erase to s1 is:"<<s1<<endl;

	string s2 = "i love you forever!";
	s2.erase(s2.begin(),s2.end());
	cout<<"after erase to s2 is"<<s2<<endl;
	return 0;
}
程序運行結果如下:


7.比較操作

    主要是一句ASCII值來比較大小。若字符串s1“大於”s2,表明兩者相比較時遇到了第一對不同的字符,字符串s1中第一個不同的字符比字符串s2中同樣位置的字符在ASCII表中位置更靠後。
    C++ STL提供了多鍾字符串比較方法,他們各具特色。其中最簡單的就是使用非成員的重載運算符函數operator==、operator!=、operator>、operator<、operator>=和operator<=。

最後更新:2017-04-04 07:03:42

  上一篇:go androidEditText不可編輯的問題
  下一篇:go 手機號碼和電話號碼的正則表達式