1.知识点

2.源程序
#include <iostream>
#include <tchar.h>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> vectA;
cout << "2.演示vector末尾的添加移除操作:\n";
vectA.push_back(1);
vectA.push_back(3);
vectA.push_back(5);
vectA.push_back(7);
vectA.push_back(9);
for (size_t i = 0; i < vectA.size(); i++)
{
cout << vectA[i] << " ";
}
cout << "\n\n";
vectA.pop_back();
for (size_t i = 0; i < vectA.size(); i++)
{
cout << vectA[i] << " ";
}
cout << "\n";
vectA.pop_back();
for (size_t i = 0; i < vectA.size(); i++)
{
cout << vectA.at(i) << " ";
}
cout << "\n\n";
cout << "3.演示vector的数据存取:\n";
for (size_t i = 0; i < vectA.size(); i++)
{
cout << "vectA["<<i<<"] = "<< vectA.at(i) << "\n";
}
cout << "\n";
vectA[1] = 155;
vectA.at(2) = 99;
for (size_t i = 0; i < vectA.size(); i++)
{
cout << "vectA[" << i << "] = " << vectA.at(i) << "\n";
}
cout << "\n";
int iFront, iBack;
iFront = vectA.front();
iBack = vectA.back();
cout << "iFront = " << iFront << endl;
cout << "iBack = " << iBack << endl << endl;
int iFront2, iBack2;
vectA.front() = 1111;
vectA.back() = 2222;
iFront2 = vectA.front();
iBack2 = vectA.back();
cout << "iFront2 = " << iFront2 << endl;
cout << "iBack2 = " << iBack2 << endl << endl;
for (size_t i = 0; i < vectA.size(); i++)
{
cout << "vectA[" << i << "] = " << vectA.at(i) << "\n";
}
cout << "\n";
vector<char> vectB;
vectB.push_back('A');
vectB.push_back('B');
vectB.push_back('C');
vectB.push_back('D');
for (size_t i = 0; i < vectB.size(); i++)
{
cout << "vectB[" << i << "] = " << vectB.at(i) << "\n";
}
cout << "\n";
char cFront, cBack;
cFront = vectB.front();
cBack = vectB.back();
cout << "cFront = " << cFront << endl;
cout << "cBack = " << cBack << endl << endl;
cout << "4.演示vector与迭代器的配合使用:\n";
vector<int> vectC;
vectC.push_back(1);
vectC.push_back(3);
vectC.push_back(5);
vectC.push_back(7);
vectC.push_back(9);
vector<int>::iterator it;
it = vectC.begin();
for (size_t i = 0; i < vectC.size(); i++)
{
cout << "vectC[" << i << "] = " << vectC.at(i) << "\n";
}
cout << "\n";
cout << "4.1利用迭代器获取容器vector的对象vectA的元素的值\n";
cout << "刚开始 *it = " << *it << endl;
++it;
cout << "后来啊 *it = " << *it << endl << endl;
cout << "刚开始 *it = " << *it << endl;
it = it + 2;
cout << "后来啊 *it = " << *it << endl << endl;
for (size_t i = 0; i < vectC.size(); i++)
{
cout << "vectC[" << i << "] = " << vectC.at(i) << "\n";
}
cout << "\n";
cout << "4.2利用迭代器修改容器vector的对象vectA的元素的值\n";
it = vectC.begin();
*it = *it + 1000;
it = it + 2;
*it = *it - 1000;
for (size_t i = 0; i < vectC.size(); i++)
{
cout << "vectC[" << i << "] = " << vectC.at(i) << "\n";
}
cout << "\n\n";
cout << "4.3利用迭代器遍历容器\n";
for (it = vectC.begin(); it != vectC.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
vector<int>::reverse_iterator r_it;
cout << "4.4利用反向迭代器遍历容器\n";
for (r_it = vectC.rbegin(); r_it != vectC.rend(); ++r_it)
{
cout << *r_it << " ";
}
cout << "\n\n";
cout << "5.vector对象的带参数构造:\n";
cout << "5.1 vector对象的带参数构造 vector(beg,end):\n";
int iArray[] = { 0,1,2,3,4 };
vector<int> vecIntA(iArray, iArray + 5);
vector<int> vecIntB(vecIntA.begin(), vecIntA.end());
for (vector<int>::iterator it = vecIntA.begin(); it != vecIntA.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
for (vector<int>::iterator it = vecIntB.begin(); it != vecIntB.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "5.2 vector对象的带参数构造 vector(n,elem):\n";
vector<int> vecIntC(3, 9);
for (vector<int>::iterator it = vecIntC.begin(); it != vecIntC.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "5.3 vector对象的带参数构造 vector(const vector& vec):\n";
vector<int> vecIntD(vecIntA);
for (vector<int>::iterator it = vecIntD.begin(); it != vecIntD.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "6.vector的赋值:\n";
cout << "6.1 vector.assign(beg,end):\n";
int iArray2[] = { 0,1,2,3,4 };
vector<int> vecIntA2(iArray2, iArray2 + 5);
vector<int> vecIntB2;
vecIntB2.assign(vecIntA2.begin(), vecIntA2.end());
for (vector<int>::iterator it = vecIntA2.begin(); it != vecIntA2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
for (vector<int>::iterator it = vecIntB2.begin(); it != vecIntB2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "6.2 vector.assign(n, elem):\n";
vector<int> vecIntC2;
vecIntC2.assign(3, 9);
for (vector<int>::iterator it = vecIntC2.begin(); it != vecIntC2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "6.3 vector& operator = (const vector &vec):\n";
vector<int> vecIntD2;
vecIntD2 = vecIntC2;
for (vector<int>::iterator it = vecIntD2.begin(); it != vecIntD2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "6.4 vector.swap(vec):\n";
vector<int> vecIntE2;
vecIntE2.push_back(1);
vecIntE2.push_back(3);
vecIntE2.push_back(5);
vecIntE2.push_back(7);
vecIntE2.push_back(9);
vector<int> vecIntF2;
vecIntF2.push_back(10);
vecIntF2.push_back(30);
vecIntF2.push_back(50);
vecIntF2.push_back(70);
vecIntF2.push_back(90);
cout << "交换前vecIntE2:\n";
for (vector<int>::iterator it = vecIntE2.begin(); it != vecIntE2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n";
cout << "交换前vecIntF2:\n";
for (vector<int>::iterator it = vecIntF2.begin(); it != vecIntF2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
vecIntE2.swap(vecIntF2);
cout << "交换后vecIntE2:\n";
for (vector<int>::iterator it = vecIntE2.begin(); it != vecIntE2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n";
cout << "交换后vecIntF2:\n";
for (vector<int>::iterator it = vecIntF2.begin(); it != vecIntF2.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.vector的大小:\n";
vector<int> vecIntG;
vecIntG.push_back(1);
vecIntG.push_back(2);
vecIntG.push_back(3);
cout << "vecIntG刚开始包含的元素:\n";
for (vector<int>::iterator it = vecIntG.begin(); it != vecIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.1 vector.size();返回容器中元素的个数:\n";
int Count1 = vecIntG.size();
cout << "vecIntG刚开始包含的元素个数是:" << Count1 << endl << endl;
cout << "7.2 vector.empty();判断容器是否为空:\n";
bool bool_vecIntG = vecIntG.empty();
if(bool_vecIntG == true)
cout << "vecIntG为空\n";
else
cout << "vecIntG不为空\n";
cout << "\n";
cout << "7.3 vector.resize(num);:\n";
vecIntG.resize(5);
for (vector<int>::iterator it = vecIntG.begin(); it != vecIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "7.4 vector.resize(num,elem);:\n";
vecIntG.resize(8,3);
for (vector<int>::iterator it = vecIntG.begin(); it != vecIntG.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "8.vector的插入:\n";
vector<int> vecIntH;
vector<int> vecIntJ;
vecIntH.push_back(1);
vecIntH.push_back(3);
vecIntH.push_back(5);
vecIntH.push_back(7);
vecIntH.push_back(9);
vecIntJ.push_back(2);
vecIntJ.push_back(4);
vecIntJ.push_back(6);
vecIntJ.push_back(8);
cout << "8.1 vector.insert(pos,elem);:\n";
vecIntH.insert(vecIntH.begin(), 11);
for(vector<int>::iterator it = vecIntH.begin(); it != vecIntH.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "8.2 vector.insert(pos,n,elem);:\n";
vecIntH.insert(vecIntH.begin() + 1, 2, 33);
for(vector<int>::iterator it = vecIntH.begin(); it != vecIntH.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "8.3 vector.insert(pos,beg,end);:\n";
vecIntH.insert(vecIntH.begin(), vecIntJ.begin(), vecIntJ.end());
for(vector<int>::iterator it = vecIntH.begin(); it != vecIntH.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "9.vector的删除:\n";
vector<int> vecIntL;
vecIntL.push_back(1);
vecIntL.push_back(3);
vecIntL.push_back(5);
vecIntL.push_back(7);
vecIntL.push_back(9);
vecIntL.push_back(11);
vecIntL.push_back(13);
vecIntL.push_back(15);
vecIntL.push_back(15);
vecIntL.push_back(15);
vecIntL.push_back(15);
vecIntL.push_back(17);
vecIntL.push_back(19);
cout << "vecIntL初始数据:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "9.1 vector.erase(pos);:\n";
vecIntL.erase(vecIntL.begin() + 1);
cout << "vecIntL现在的数据:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "9.2 vector.erase(beg,end);:\n";
vector<int>::iterator iter1 = vecIntL.erase(vecIntL.begin(), vecIntL.begin() + 3);
cout << "此时*iter1的值是: " << *iter1 << endl;
cout << "vecIntL现在的数据:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "9.3 删除容器中所有等于15的元素:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); )
{
if (*it == 15)
{
it = vecIntL.erase(it);
}
else
{
++it;
}
}
cout << "vecIntL现在的数据:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); ++it)
{
cout << *it << " ";
}
cout << "\n\n";
cout << "9.4 void vector.clear();:\n";
cout << "vecIntL大小:" << vecIntL.size() << endl;
vecIntL.clear();
cout << "vecIntL现在的数据:\n";
for (vector<int>::iterator it = vecIntL.begin(); it != vecIntL.end(); ++it)
{
cout << *it << " ";
}
cout << "vecIntL现在大小:" << vecIntL.size() << endl;
cout << "\n\n";
return 0;
}
3.运行结果
