阅读以下说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。 【

最全题库2022-08-02  17

问题 阅读以下说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。【说明】   C++标准模板库中提供了vector模板类,可作为动态数组使用,并可容纳任意数据类型,其所属的命名空间为std。vector模板类的部分方法说明如下表所示:【C++代码】  #include <iostream> #include <vector> using namespace  (1) ; typedef vector< (2) > INTVECTOR; const int ARRAY_SIZE = 6; void ShowVector(INTVECTOR &theVector); int main(){   INTVECTOR theVector;   // 初始化theVector,将theVector的元素依次设置为0至5   for (int cEachItem = 0; cEachItem < ARRAY_SIZE; cEachItem++)   theVector.push_back( (3) );  ShowVector(theVector);    // 依次输出theVector中的元素   theVector.erase(theVector.begin() + 3);    ShowVector(theVector); }  void ShowVector(INTVECTOR &theVector) {    if (theVector.empty())  {       cout << "theVector is empty." << endl;    return;    }   INTVECTOR::iterator (4) ;   for(theIterator = theVector.begin(); theIterator != theVector.end(); theIterator++){        cout << *theIterator;   if (theIterator != theVector.end()-1)    cout << ", ";   }     cout << endl; }  该程序运行后的输出结果为:(5)

选项

答案

解析 (1)std   (2) int   (3)cEachItem  (4)theIterator  (5)0,1,2,4,5

本题主要考查C++语言的基本使用以及类库的应用。
    在使用标准C++库中所提供的对象时,一般需要引用标准的命名空间。所以空(1)需要填入标准的命名空间std。空(2)处主要考查是否会使用C++提供的模板类。C++中Vector模板类可存储任意类型,在定义Vector模板类的对象时,需要指定Vector对象的类型。从后面的代码可以看出,Vector被用于存储整型数,所以,空(2)处应填写整型int。初始化代码将0到5共6个整数存储到theVector对象中,所以,空(3)处将循环变量的值存入theVector中。空(4)处代码部分主要是循环输出theVector对象的内容,使用了迭代器的访问方式,因此空(4)处应该为定义迭代器变量,在后续的循环中使用该变量。程序运行时将首先输出0至5,其次会删除第3个元素,再次输出时将不再包含整数3。
转载请注明原文地址:https://tihaiku.com/congyezige/2427898.html

最新回复(0)