C++ Vector的基本使用
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
void vectorDelete(int nIndex, vector<T>& vDatas)
{
std::vector<T>::iterator it = vDatas.begin() + nIndex;
vDatas.erase(it);
}
template<typename T>
void vectorShow(vector<T>& vDatas)
{
vector<T>::iterator pos;
for (pos = vDatas.begin(); pos != vDatas.end(); pos++)
{
cout << *pos << "\t";
}
cout << endl;
}
int main()
{
cout << "Vector 的使用" << endl;
vector<int> vIntDatas;
vIntDatas.push_back(10);
vIntDatas.push_back(8);
vIntDatas.push_back(6);
vIntDatas.push_back(4);
vIntDatas.push_back(2);
vector<int>::iterator pos;
for (pos = vIntDatas.begin(); pos != vIntDatas.end(); pos++)
{
cout << *pos << "\t";
}
cout << endl;
cout << "删除 索引 1的数据" << endl;
vectorDelete<int>(1, vIntDatas);
vectorShow<int>(vIntDatas);
cout << "修改索引 0 数据" << endl;
vIntDatas[0] = 100;
vectorShow<int>(vIntDatas);
cout << "交换 索引[0]-索引[3]的数据" << endl;
swap(vIntDatas[0], vIntDatas[3]);
vectorShow<int>(vIntDatas);
vector<int>().swap(vIntDatas);
system("Pause");
return 0;
}