资源预览内容
第1页 / 共10页
第2页 / 共10页
第3页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
精品文档_初级程序员-填空集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#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中的元素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()-1)cout”,”;coutendl;该程序运行后的输出结果为: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#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中的元素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()-1)cout”,”;coutendl;该程序运行后的输出结果为: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+代码#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中的元素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()-1)cout”,”;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+代码#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中的元素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()-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指向的元素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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号