使用可带任何类型的vector是容易的事情
创建可以存放不同类型的vector,只需要把类型当作模板参数就可以
创建可以存放不同类型的vector,只需要把类型当作模板参数就可以
尖括号的参数是模板参数
//: C02:Intvector.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// Creating a vector that holds integers
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
for(int i = 0; i < 10; i++)
v.push_back(i);
for(int i = 0; i < v.size(); i++)
cout << v[i] << ", ";
cout << endl;
for(int i = 0; i < v.size(); i++)
v[i] = v[i] * 10; // Assignment
for(int i = 0; i < v.size(); i++)
cout << v[i] << ", ";
cout << endl;
getchar();
} ///:~
v[i] = v[i] * 10;
vector不仅限于输入输出,还可以使用下标操作符向vector赋值
vector是灵活的暂存器,处理对象集
输出
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90,