C++中string和vector用法總結
string
包含標頭檔案:#include<string>
申明名稱空間:using std::string ;
1) 初始化
string s1; //Default initialization; s1 is an empty string
string s2(s1); //copy s1 to s2
string s3=s1; //copy s1 to s3
string s4(“value”); //Direct initialization,s4 is a copy ofthe string literal(not including null)
string s5=”value”;//Equivalent to s4
string s6(n, ’c’);//Directly initialize s4 with n copies ofthe character ‘c’.
2) 操作(os—outputstream, is—input stream)
表1 string常用的操作
os<<s |
Write s onto the output stream os, Return os |
is>>s |
Reads whitespace-separated string from is into s, Return is |
getline(is,s) |
Reads a line of input from is into s. Return is. |
s.empty() |
Returns true if s is empty; otherwise returns false |
s.size() |
Returns the number of characters in s. (use type string::size_type) |
s[n] |
Returns a reference to the char at position n in s, position starts at 0 |
s1+s2 |
Returns a string that is the concatenation of s1 and s2 |
s1=s2 |
Replaces s1 with s2 |
s1==s2 |
The strings s1 and s2 are equal if they contain the same characters. Equality is case-sensitive |
s1!=s2 |
|
<,<=,>,>= |
Comparison are case-sensitive and use dictionary ordering |
注意事項:
string s1 = “Hello”;
string s4 = s1 + “, ”;//Ok
string s5 = “Hello” + “, ”; //Error
string s6 = s1+”, ”+ “world”;//Ok
string s7 = “Hello”+”, ”+s2;//Error
總結:每個加號的運算元都必須至少有一個是string型別的
3) 操作string裡面的每個字母
需要包含的庫檔案:#include<cctype>
表2 操作字母的庫函式
isalnum(c) |
true if c is a letter or digit |
isalpha(c) |
true if c is a letter |
iscntrl(c) |
true if c is a control character |
isdigit(c) |
true if c is a digit |
isgraph(c) |
true if c is not a space but is printable |
islower(c) |
true if c is a lowercase letter |
isprint(c) |
true if c is a printable character(a space or character that has a visible representation) |
ispunct(c) |
|
isspace(c) |
|
isupper(c) |
|
isxdigit(c) |
|
tolower(c) |
|
toupper(c) |
vector
包含的庫檔案:#include<vector>
申明名稱空間:using std::vector
1) vector的初始化
表3 vector的初始化方式
vector<T> v1 |
vector holds objects of type T. v1 is empty |
vector<T> v2(v1) |
v2 has a copy of each element in v1 |
vector<T> v2=v1 |
Equivalent to v2(v1) |
vector<T> v3(n,val) |
v3 has n elements with value val |
vector<T> v4(n) |
v4 has n copies of default-initialized object |
vector<T> v5{a,b,c,d…} |
|
vector<T> v6={a,b,c…} |
注意事項:
vector<int> v1(10);//10 elements with value 0;
vector<int> v2{10};//1 element with value 10
vector<int> v3(10,1);//10 elements with value 1;
vector<int> v4{10,1};//2 elements with value 10 and1;
vector<string> v7{10};//same as vector<string>v7(10)
vector<string> v8{10,”hi”};// same asvector<string> v8(10,”hi”)
綜上所述:當我們使用{}對vector初始化時,如果可以的話編譯器先使用列表初始化vector(list initialize),如果{}內的值不能用來初始化vector的成員,則將{}看待為(),然後再對vector進行初始化。
2) vector的操作
表4 vector的常用操作
v.empty() |
|
v.size() |
|
v.push_back(t) |
Adds an element with value t to the end of v |
v[n] |
|
v1=v2 |
|
v1={a,b,c,d…} |
|
v1==v2 v1!=v2 |
v1 and v2 are equal if they have the same number of elements and each element in v1 is equal to the corresponding element in v2 |
<,<=,>,>= |
3) iterators(迭代器)在vector中的使用
注意事項:由於vector可以動態的新增成員,在任何改變了vector大小的操作後,該vector所有的iterator都將失效。所以在任何使用iterator的迴圈中,不能夠使用如push_back等能夠改變vector大小的操作。
a) iterator的使用
vector<int> v{1,2,3,4,5};
vector<int>::iterator b=v.begin(),e=v.end();//返回vector v的起始iterator和結束iterator,其中e指向一個不存在的物件,
表5 所有iterator支援的操作
*iter |
Returns a reference to the element denoted by the iterator iter |
iter->mem |
Equivalent to (*iter).mem |
++iter |
|
--iter |
|
iter1==iter2 iter1!=iter2 |
Two iterators are equal if they denote the same element or if they are off-the-end iterator(v.end()) iterator for the same container |
表6 string和vector另外支援的操作
iter+n iter-n |
iter1+=n |
iter1-=n |
iter1-iter2 |
>,>=,<,<= |
注意事項:iterator並沒有定義iter1+iter2的運算,所以有如下的錯誤:
mid=v.begin()+(v.end()- v.begin())/2;//計算vector的中間元素,正確
mid=(v.begin()+v.end())/2;//錯誤
b) iterator的型別
我們無從知道iterator的具體型別,但是提供iterator的庫通常會提供iterator的型別,如:vector<T>::iterator和vector<T>::const_iterator
const_iterator和const pointer類似,都無法通過該變數來改變物件的值。如果一個vector或者string是const型別的,那麼我們只能用const_iterator;如果是nonconst型別的,const_iterator和iterator都可以使用。