#include <iostream>
#include <vector>
using namespace std;
/*Vector 作为函数的参数或者返回值时,需要注意它的写法,其中的“&”绝对不能少!!!:
double Distance(vector<int>&a, vector<int>&b)
*/
int main()
{
/*
vector 是同一种类型的对象的集合,每个对象都有一个对应的整数索引值。和 string 对象一样,标准库将负责管理与存储元素相关的内存。
我们把 vector 称为容器,是因为它可以包含其他对象。一个容器中的所有对象都必须是同一种类型的。
size是当前vector容器真实占用的大小,也就是容器当前拥有多少个容器。
capacity是指在发生realloc前能允许的最大元素数,即预分配的内存空间。
当然,这两个属性分别对应两个方法:resize()和reserve()。
使用resize(),容器内的对象内存空间是真正存在的。
使用reserve()仅仅只是修改了capacity的值,容器内的对象并没有真实的内存空间(空间是"野"的)。
*/
vector<int> V0; // V0为空,建立一个vector,int为数组元素的数据类型,V0为动态数组名;向量大小: vec.size()
cout << "Vector V0 has " << V0.size() << " elements." << endl;
cout << "Vector V0 can hold " << V0.capacity() << " elements." << endl;
// 在向量尾部插入一个元素
V0.push_back(10);
cout << "Vector V0 has " << V0.size() << " elements." << endl;
cout << "Vector V0 can hold " << V0.capacity() << " elements." << endl;
// 删除向量最后一个元素
V0.pop_back();
cout << "Vector V0 has " << V0.size() << " elements." << endl;
cout << "Vector V0 can hold " << V0.capacity() << " elements." << endl;
vector<int> V(10); // 构造包含10个元素的向量;
cout << "Vector V has " << V.size() << " elements." << endl;
cout << "Vector V can hold " << V.capacity() << " elements." << endl;
for (vector<int>::size_type i = 0; i < V.size(); i++) //下标是从0开始的。
{
V[i] = i; // 使用运算[]存取向量的元素
}
cout << "Vector V has " << V.size() << " elements." << endl;
cout << "Vector V can hold " << V.capacity() << " elements." << endl;
// vector有4中变量类型:iterator,const_iterator,reverse_iterator,const_reverse_iterator,这里需要使用限定符号::
vector<int>::iterator itr;
// begin()返回指向第一个元素的迭代器;end()返回指向最后一个元素下一位置的迭代器;;front() 返回第一个元素的引用,back() 返回最后一个元素的引用
for (itr = V.begin(); itr != V.end(); itr++)
{
cout << (*itr) << " "; // 使用迭代器存取向量的元素
}
cout << endl;
for (itr = V.begin(); itr != V.end(); itr++)
{
*itr = 2 * (*itr);
}
cout << "Now the elements are:" << endl;
for (itr = V.begin(); itr != V.end(); itr++)
{
cout << (*itr) << " ";
}
cout << endl;
vector<int>::const_iterator citr;
cout << "Now the elements are:" << endl;
for (citr = V.begin(); citr != V.end(); citr++)
{
cout << (*citr) << " ";
}
cout << endl;
itr = V.begin();
itr++; //itr指向元素V[1]
vector<int>::iterator p;
p = V.insert(itr, 100); //在V[1]前插入100
cout << "After insertion the elements are:" << endl;
for (citr = V.begin(); citr != V.end(); citr++)
{
cout << (*citr) << " ";
}
cout << endl;
V.erase(p); //删除p所指向的元素,即100
cout << "After deletion1 the elements are:" << endl;
for (citr = V.begin(); citr != V.end(); citr++)
{
cout << (*citr) << " ";
}
cout << endl;
vector<int>::iterator r1 = (V.begin() + 1);
*r1 = 110; //如果类型是iterator,可通过迭代器改变所指向的对象;const_iterator类型不可通过迭代器修改向量的元素,通常用于函数的参数使用了vectir的场景
cout << "After change the elements are:" << endl;
for (citr = V.begin(); citr != V.end(); citr++)
{
cout << (*citr) << " ";
}
cout << endl;
V.clear(); //删除所有元素
cout << "After clear the elements are:" << endl;
cout << "Vector V has " << V.size() << " elements." << endl;
cout << "Vector V can hold " << V.capacity() << " elements." << endl;
}
输出:
Vector V0 has 0 elements.
Vector V0 can hold 0 elements.
Vector V0 has 1 elements.
Vector V0 can hold 1 elements.
Vector V0 has 0 elements.
Vector V0 can hold 1 elements.
Vector V has 10 elements.
Vector V can hold 10 elements.
Vector V has 10 elements.
Vector V can hold 10 elements.
0 1 2 3 4 5 6 7 8 9
Now the elements are:
0 2 4 6 8 10 12 14 16 18
Now the elements are:
0 2 4 6 8 10 12 14 16 18
After insertion the elements are:
0 100 2 4 6 8 10 12 14 16 18
After deletion1 the elements are:
0 2 4 6 8 10 12 14 16 18
After change the elements are:
0 110 4 6 8 10 12 14 16 18
After clear the elements are:
Vector V has 0 elements.
Vector V can hold 20 elements.