14-模板类vector
16.3.1 模板类vector
第4章简要地介绍了vector类,下面更详细地介绍它。在计算中,矢量(vector)对应数组,而不是第11章介绍的数学矢量(在数学中,可以使用N个分量来表示N维数学矢量,因此从这方面讲,数学矢量类似一个N维数组。然而,数学矢量还有一些计算机矢量不具备的其他特征,如内乘积和外乘积)。计算矢量存储了一组可随机访问的值,即可以使用索引来直接访问矢量的第10个元素,而不必首先访问前面第9个元素。所以vector类提供了与第14章介绍的valarray和ArrayTP以及第4章介绍的array类似的操作,即可以创建vector对象,将一个vector对象赋给另一个对象,使用[ ]运算符来访问vector元素。要使类成为通用的,应将它设计为模板类,STL正是这样做的——在头文件vector(以前为vector.h)中定义了一个vector模板。
要创建vector模板对象,可使用通常的
#include vector
using namespace std;
vector<int> ratings(5); // a vector of 5 ints
int n;
cin >> n;
vector<double> scores(n); // a vector of n doubles
由于运算符[ ]被重载,因此创建vector对象后,可以使用通常的数组表示法来访问各个元素:
ratings[0] = 9;
for (int i = 0; i < n; i++)
cout << scores[i] << endl;
分配器
与string类相似,各种STL容器模板都接受一个可选的模板参数,该参数指定使用哪个分配器对象来管理内存。例如,vector模板的开头与下面类似:
template <class T, class Allocator = allocator<T> >
class vector {...
如果省略该模板参数的值,则容器模板将默认使用allocator
程序清单16.7是一个要求不高的应用程序,它使用了这个类。该程序创建了两个vector对象—— 一个是int规范,另一个是string规范,它们都包含5个元素。
程序清单16.7 vect1.cpp
// vect1.cpp -- introducing the vector template
#include <iostream>
#include <string>
#include <vector>
const int NUM = 5;
int main()
{
using std::vector;
using std::string;
using std::cin;
using std::cout;
using std::endl;
vector<int> ratings(NUM);
vector<string> titles(NUM);
cout << "You will do exactly as told. You will enter\n"
<< NUM << " book titles and your ratings (0-10).\n";
int i;
for (i = 0; i < NUM; i++)
{
cout << "Enter title #" << i + 1 << ": ";
getline(cin,titles[i]);
cout << "Enter your rating (0-10): ";
cin >> ratings[i];
cin.get();
}
cout << "Thank you. You entered the following:\n"
<< "Rating\tBook\n";
for (i = 0; i < NUM; i++)
{
cout << ratings[i] << "\t" << titles[i] << endl;
}
return 0;
}
程序清单16.7中程序的运行情况如下:
You will do exactly as told. You will enter
5 book titles and your ratings (0-10).
Enter title #1: The Cat Who Knew C++
Enter your rating (0-10): 6
Enter title #2: Felonious Felines
Enter your rating (0-10): 4
Enter title #3: Warlords of Wonk
Enter your rating (0-10): 3
Enter title #4: Don't Touch That Metaphor
Enter your rating (0-10): 5
Enter title #5: Panic Oriented Programming
Enter your rating (0-10): 8
Thank you. You entered the following:
Rating Book
6 The Cat Who Knew C++
4 Felonious Felines
3 Warlords of Wonk
5 Don't Touch That Metaphor
8 Panic Oriented Programming
该程序使用vector模板只是为方便创建动态分配的数组。下一节将介绍一个使用更多类方法的例子。