c++基础复习(vector string)

The C++ standard defines two classes: the vector and string. vector is intended to replace the built-in C++ array, which causes no end of troube. The problem with the built in C++ array is that it does not behave like a first class object. For instance, built-in arrays cannot be copied with =, a built in array does not remember how many items it can store, and its indexing operator does not check that the index is valid. The built in string is simply an array of characters, and thus has the liabilities of arrays plus a few more, For instance, == does not correctly compare two built-in strings.

The vector and string classes in the STL treat arrays and strings as first-class objects. A vector knows how large it is. Two string objects can be compared with ==,<,and so on. Both vector and string canbe copied with =. if possible, you should avoid using the built in C++ array and string.

Vector and string are easy to use. Notice also that size is method that returns the size of the vector. In many cases, the inital size is 0 and the vector grows as needed.

C++ has long allowed initialization of built in C++ arrays:

int daysInMonth[]={31,28,..........31};

It was annoying that this syntax was not legal for vectors. In older C++, vectors were either initialized with size 0 or possibly by specifying a size. for intance, we would write:

vector<int>dayInMonth(12);//No {}before C++11;

daysInMonth[0]=31;......;

Certainly this leaves something to be desired .C++11 fixes this problem and allows;

vector<int> daysInMonth={31,......31};

Requiring the = in the initialization violates the spirit of uniform initialization, since noew we would have to remember when it would be appropriate to use =. Consequently, C++11 also allows (and some prefer);

vector<int> daysInMonth{31,......31};

With syntax, however, comes ambiguity, as one sees with the declration:

vector<int>dasInMontj{12};

Is this a vector of size 12, or is it a vector of size 1 with a single element 12 in position 0? C++11gives precedence to the initalizer list, so in fact this is a vector of size 1 with a single element 12 in position 0, and if the intention is to initialize a vector of size 12, the old C++ syntax using parentheses must be used:

vector<int> daysInMonth(12);//Must use () to call constructor that takes size 

string is also easy to use and has allthe relational and queality operators to compare the states of two strings. Thus str1==str2is true if the values of the strings are the same. It also has a length method that returns the string length.

As the shows , the basic operation on arrays is indexing with []. Thus, the sum of the squares can be computed as:

int sum=0;

for(int i=0;i<squares.size();++i)

      sum+=squares[i];

The pattern of accessing every element sequentially in a collection such as an array or a vector is fundamental, and using array indexing for this purpose does not clearly express the idiom. C++11 adds range for syntax for this purpose. The above fragment can be written instead as:

int sum = 0;

for(int x:squares)

    sum+=x;

In many cases, the declaration of the type in the range for statement is unneeded; if squares is a vector<int>, it is obvious that x is intended to be in int. Thus C++11 also allows the use of the reserved word auto to signify that the compiler will automatically infer the appropriate type:

int sum = 0;

for(auto x : squares)

    sum += x;

The range for loop is appropriate only if every item is being accessed sequentially and only if the index is not needed .


我自己写的小网站www.caozhicong.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值