第一部分 基本语言 第三章 标准库类型(3.3标准库vector类型)

3.3标准库vector类型


一、vector

vector是同一种类型对象的集合,对每个对象都有一个对应的整数索引值。标准库负责管理和存储相关内存,我们把vector成为容器,它可以包含其他对象,一个容器中的所有对象都必须是同一种类型。这里贴一段来自http://www.cplusplus.com/reference/vector/vector/的介绍。讲得更加清楚。

Vector
Vectors are sequence containers representing arrays that can change in size.

Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

Compared to the other dynamic sequence containers (dequeslists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

vector是一个类模版,可以用来编写不同数据类型的类定义或是函数定义。必须说明vector保存何种对象的类型,形式:vector<数据类型>类型名;


二、vector对象的定义和初始化: 

vector类型模版定义了几种构造函数来初始化vector对象。

vector<T> v1保存类型为T的对象,默认构造函数,v1为空。

vector<T> v2(v1)v2是v1的一个副本。

vector<T> v3(n,i) n个值为i的元素。

vector<T> v4(n) v4是含有初始化值的n个副本。

创建非空的vector对象需要给出初始化值,当把一个vector对象复制到另一个vector对象时,新复制的vector中的每一个元素都初始化为原来vector中对应值的副本,前提是这两个vector对象保持同一种类型

      如果没有指定值的初始化式,vector将根据对象类型提供相应的构造函数进行初始化,对于没有定义构造函数的情况,vector仍将提供一个初始值。



习题解答:

1、下面哪些vector定义不正确?

vector<vector<int>> ivec;

vector<string> svec=ivec;

vector<string> svec(10,"null");


答:第二个不正确,第二个用vector<int>型vector初始化stringvector,类型不一致。

2、下面列每个vector对象中的元素个数是多少?各元素的值是什么?

vector<int> ivecl;

vector<int> ivec2(10);

vector<int> ivec3(10,42);

vector<string> svec1;

vector<string> svec2(10);

vector<string> svec3(10,"hello");


答:1、空int型对象。

2、vector中有十个初始化为0的int值。

3、vector中有十个初始化为42的int值。

4、空string对象。

5、vector中10个空串。

6、vector中10个初始化为“hello”的字符串。


三、vector提供的操作:

1、vector对象提供了许多类似于前面所说的string对象的操作。基本合前面string类似。

2、vector对象的size,成员函数size返回相应的vector类定义的size_type类型的值。

3、向vector添加元素,push_back()操作接受一个元素值,并将他的一个新元素添加到vector容器的后面

程序示例:

#include "Header.h"


int main(int argc,constchar * argv[])

{

/*vector<int> v3(3);

for (vector<int>::size_type n=0; n!=v3.size(); ++n) {

cout<<v3[n]<<endl;

}*/

string word;

vector<string> text;

cout<<"please enter the string word:"<<endl;

while (cin>>word) {

if (word=="0") {

break;

}

text.push_back(word);

//cout<<text[0]<<endl;

}

for (vector<string>::size_type n=0; n!=text.size(); ++n) {

cout<<text[n];

}

}


这里要注意的是:文件结束符,在Windows系统下是control+Z,unix下是control+D。

4、vector的下标操作:

类似于string类型的下标操作。但是vector的下标不能用来添加元素,只能用来读取元素。必须是已经存在的元素才能用下标操作符进行索引。





习题解答:

1、读入一组整数到vector对象,计算并输出每对相邻元素的和,如果读入个数为奇数,则提示用户最后一个元素没有求和,并输出其值。然后修改程序,头尾元素两两配对,计算每对元素的和并输出。

程序示例:

#include "Header.h"


int main(int argc,constchar * argv[])

{

int number;

vector<int> array;

cout<<"please enter the sequence of number:"<<endl;

while (cin>>number) {

if (number==0) {

break;

}

array.push_back(number);

}

//如果输入数字奇数个提醒用户最后一个没有求和

if (array.size()%2) {

cout<<"the last number haven't sum"<<endl;

return 0;

}

for (vector<int>::size_type n = 0; n!=array.size()-1; ++n) {

int g;

g=array[n]+array[n+1];

cout<<"the two number sum"<<g<<" "<<endl;

}

}


输出结果:

please enter the sequence of number:

123 234 345 456 0

the two number sum357 

the two number sum579 

the two number sum801 


修改后的程序:

#include "Header.h"


int main(int argc,constchar * argv[])

{

int number;

vector<int> array;

cout<<"please enter the sequence of number:"<<endl;

while (cin>>number) {

if (number==0) {

break;

}

array.push_back(number);

}

//如果输入数字奇数个提醒用户最后一个没有求和

if (array.size()%2) {

cout<<"the last number haven't sum"<<endl;

return0;

}

for (vector<int>::size_type n = 0; n<=array.size()/2-1; ++n) {

int g;

g=array[n]+array[array.size()-1-n];//由于sizevector中的元素个数,而下标要顺序减一。

cout<<"the two number sum: "<<g<<endl;

}

}


输出结果:

please enter the sequence of number:

123 234 345 456 0

the two number sum: 579

the two number sum: 579


2、读入一段文本到vector对象,每个单词存储为vector的一个元素,并把vector对象中的每个单词转化为大写字母,每八个单词为一行输出。

#include <iostream>

#include <string.h>

#include <cctype>

#include <vector>


using namespace std;


int main(int argc, const char * argv[])

/*

 **输入文本

 */

{

vector<string> article;

string word;

cout<<"please enter the article:"<<endl;

while (cin>>word) {

article.push_back(word);

}

/*

**置空标点

*/


for (vector<string>::size_type n=0; n!=article.size(); ++n) {

for (string::size_type j=0; j<=article[n].size(); ++j) {

if (ispunct(article[n][j])) {

article[n][j]=NULL;

}

article[n][j]=article[n][j]-32;

}


}

for (vector<string>::size_type n=0; n!=article.size(); ++n) {

cout<<" "<<article[n]<<" ";

}

}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值