C++中string和vector用法总结

string

包含头文件:#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)

 

 

vector

包含的库文件:#include

申明命名空间:using std::vector

1)       vector的初始化

表3 vector的初始化方式

vector v1

vector holds objects of type T. v1 is empty

vector v2(v1)

v2 has a copy of each element in v1

vector v2=v1

Equivalent to v2(v1)

vector v3(n,val)

v3 has n elements with value val

vector v4(n)

v4 has n copies of default-initialized object

vector v5{a,b,c,d…}

 

vector v6={a,b,c…}

 

 

注意事项:

vector v1(10);//10 elements with value 0;

vector v2{10};//1 element with value 10

 

vector v3(10,1);//10 elements with value 1;

vector v4{10,1};//2 elements with value 10 and1;

 

vector v7{10};//same as vectorv7(10)

vector v8{10,”hi”};// same asvector 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 v{1,2,3,4,5};

vector::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::iterator和vector::const_iterator

const_iterator和const pointer类似,都无法通过该变量来改变对象的值。如果一个vector或者string是const类型的,那么我们只能用const_iterator;如果是nonconst类型的,const_iterator和iterator都可以使用。

你可能感兴趣的:(C++中string和vector用法总结)