《初级程序员-填空集_10(精选试题)》由会员分享,可在线阅读,更多相关《初级程序员-填空集_10(精选试题)(10页珍藏版)》请在金锄头文库上搜索。
1、精品文档_初级程序员-填空集1、说明C+标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空问为std。vector模板类的部分方法说明如表12-3所示。表12-3vector模板类的部分方法方法含义push_back(k)向vector对象的尾部添加一个元素kbegin()返回一个迭代器对象,该对象指向vector中的第一个元素end()返回一个迭代器对象,该对象指向vector中的最后一个元素empty()测试vector对象是否为空erase(ptr)删除vector中ptr指向的元素C+代码#includeiostream#includevec
2、torusingnamespace(1);typedefvector(2)INTZECTOR;constintARRAY_SIZE=6;voidShowVector(INTVECTOR&theVector);intmain()INTVECTORtheVector;/初始化theVector,将theVector的元素依次设置为0至5for(intcEachItem=0;cEachItemARRAY_SIZE;cEachItem+)theVector.push_back(3);ShowVector(theVector);/依次输出theVector中的元素theVector.erase(theV
3、ector.begin()+3);ShowVector(theVector);voidShowVector(INTVECTOR&theVector)if(theVector.empty()cout”theVectorisempty.”endl;return;INTVECTOR:iterator(4);for(theIterator=theVector.begin();theIterator!=theVector.end();theIterator+)cout*theIterator;if(theIterator!=thevector.end()-1)cout”,”;coutendl;该程序运行
4、后的输出结果为:0,1,2,3,4,5(5)2、说明C+标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空问为std。vector模板类的部分方法说明如表12-3所示。表12-3vector模板类的部分方法方法含义push_back(k)向vector对象的尾部添加一个元素kbegin()返回一个迭代器对象,该对象指向vector中的第一个元素end()返回一个迭代器对象,该对象指向vector中的最后一个元素empty()测试vector对象是否为空erase(ptr)删除vector中ptr指向的元素C+代码#includeiostream#in
5、cludevectorusingnamespace(1);typedefvector(2)INTZECTOR;constintARRAY_SIZE=6;voidShowVector(INTVECTOR&theVector);intmain()INTVECTORtheVector;/初始化theVector,将theVector的元素依次设置为0至5for(intcEachItem=0;cEachItemARRAY_SIZE;cEachItem+)theVector.push_back(3);ShowVector(theVector);/依次输出theVector中的元素theVector.er
6、ase(theVector.begin()+3);ShowVector(theVector);voidShowVector(INTVECTOR&theVector)if(theVector.empty()cout”theVectorisempty.”endl;return;INTVECTOR:iterator(4);for(theIterator=theVector.begin();theIterator!=theVector.end();theIterator+)cout*theIterator;if(theIterator!=thevector.end()-1)cout”,”;couten
7、dl;该程序运行后的输出结果为:0,1,2,3,4,5(5)3、说明C+标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空问为std。vector模板类的部分方法说明如表12-3所示。表12-3vector模板类的部分方法方法含义push_back(k)向vector对象的尾部添加一个元素kbegin()返回一个迭代器对象,该对象指向vector中的第一个元素end()返回一个迭代器对象,该对象指向vector中的最后一个元素empty()测试vector对象是否为空erase(ptr)删除vector中ptr指向的元素C+代码#includeios
8、tream#includevectorusingnamespace(1);typedefvector(2)INTZECTOR;constintARRAY_SIZE=6;voidShowVector(INTVECTOR&theVector);intmain()INTVECTORtheVector;/初始化theVector,将theVector的元素依次设置为0至5for(intcEachItem=0;cEachItemARRAY_SIZE;cEachItem+)theVector.push_back(3);ShowVector(theVector);/依次输出theVector中的元素theV
9、ector.erase(theVector.begin()+3);ShowVector(theVector);voidShowVector(INTVECTOR&theVector)if(theVector.empty()cout”theVectorisempty.”endl;return;INTVECTOR:iterator(4);for(theIterator=theVector.begin();theIterator!=theVector.end();theIterator+)cout*theIterator;if(theIterator!=thevector.end()-1)cout”,
10、”;coutendl;该程序运行后的输出结果为:0,1,2,3,4,5(5)4、说明C+标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空问为std。vector模板类的部分方法说明如表12-3所示。表12-3vector模板类的部分方法方法含义push_back(k)向vector对象的尾部添加一个元素kbegin()返回一个迭代器对象,该对象指向vector中的第一个元素end()返回一个迭代器对象,该对象指向vector中的最后一个元素empty()测试vector对象是否为空erase(ptr)删除vector中ptr指向的元素C+代码#in
11、cludeiostream#includevectorusingnamespace(1);typedefvector(2)INTZECTOR;constintARRAY_SIZE=6;voidShowVector(INTVECTOR&theVector);intmain()INTVECTORtheVector;/初始化theVector,将theVector的元素依次设置为0至5for(intcEachItem=0;cEachItemARRAY_SIZE;cEachItem+)theVector.push_back(3);ShowVector(theVector);/依次输出theVector
12、中的元素theVector.erase(theVector.begin()+3);ShowVector(theVector);voidShowVector(INTVECTOR&theVector)if(theVector.empty()cout”theVectorisempty.”endl;return;INTVECTOR:iterator(4);for(theIterator=theVector.begin();theIterator!=theVector.end();theIterator+)cout*theIterator;if(theIterator!=thevector.end()-
13、1)cout”,”;coutendl;该程序运行后的输出结果为:0,1,2,3,4,5(5)5、说明C+标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空问为std。vector模板类的部分方法说明如表12-3所示。表12-3vector模板类的部分方法方法含义push_back(k)向vector对象的尾部添加一个元素kbegin()返回一个迭代器对象,该对象指向vector中的第一个元素end()返回一个迭代器对象,该对象指向vector中的最后一个元素empty()测试vector对象是否为空erase(ptr)删除vector中ptr指向的元
14、素C+代码#includeiostream#includevectorusingnamespace(1);typedefvector(2)INTZECTOR;constintARRAY_SIZE=6;voidShowVector(INTVECTOR&theVector);intmain()INTVECTORtheVector;/初始化theVector,将theVector的元素依次设置为0至5for(intcEachItem=0;cEachItemARRAY_SIZE;cEachItem+)theVector.push_back(3);ShowVector(theVector);/依次输出theVector中的元素theVecto