包含头文件:#include
申明命名空间: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< |
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
表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) |
|
包含的库文件:#include
申明命名空间:using std::vector
1) vector的初始化
表3 vector的初始化方式
vector |
vector holds objects of type T. v1 is empty |
vector |
v2 has a copy of each element in v1 |
vector |
Equivalent to v2(v1) |
vector |
v3 has n elements with value val |
vector |
v4 has n copies of default-initialized object |
vector |
|
vector |
|
注意事项:
vector
vector
vector
vector
vector
vector
综上所述:当我们使用{}对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
vector
表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
const_iterator和const pointer类似,都无法通过该变量来改变对象的值。如果一个vector或者string是const类型的,那么我们只能用const_iterator;如果是nonconst类型的,const_iterator和iterator都可以使用。