vector 包含着一系列连续存储的元素,其行为和数组类似,顺序存储结构
可以快速访问vector中的任意元素,也可以从末尾添加元素或删除元素
其他位置插入和删除较慢
构造函数
vector<int> myVector(5, 10);//5个元素,每个元素为10
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
vector<int> myVector2(myVector);//另一个vector作为参数
ite = myVector2.begin();
for (ite; ite != myVector2.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
ite = myVector2.begin();
ite++;
ite += 2;
vector<int> myVector3(ite, myVector2.end());//另一个vector作为参数
ite = myVector3.begin();
for (ite; ite != myVector3.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
访问或修改vector中某个下标特定元素
1.使用[]
vector<int> myVector(5, 10);//5个元素,每个元素为10
vector<int>::iterator ite = myVector.begin();
cout << myVector[5] << "\n";//这样会报错,越界了
myVector[5] = 100;//这样会报错,越界了
2.使用at()方法
如果产生越界,那么抛出异常out_of_range
vector<int> myVector(5, 10);//5个元素,每个元素为10
vector<int>::iterator ite = myVector.begin();
//cout << myVector[5] << "\n";
//myVector[5] = 100;
try
{
//修改下标为5的元素
myVector.at(5) = 200;
}
catch (out_of_range)
{
cout << "越界异常" << '\n';
return 0;
}
catch (...)
{
cout << "未知异常" << '\n';
}
运算符
v1 == v2 //比较形式和string一样
v1 != v2
v1 <= v2
v1 >= v2
v1 < v2
v1 > v2
v[]
assign() 给vector重新赋值,之前的元素全部清除,在赋值
vector<int> myVector(5, 10);//5个元素,每个元素为10
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
myVector.assign(3, 2);//重新赋值为3个元素,每个元素为2
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
myVector.assign(8, 0);//重新赋值为6个元素,每个元素为0
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
front()返回vector起始元素
back()返回vector的最后一个元素
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
cout << "起始元素: " << myVector.front() << "\n";
cout << "最后一个元素: " << myVector.back() << "\n";
push_back(),在末尾添加元素
pop_back(),尾删除
注:没有头添加和头删除
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
//尾添加
myVector.push_back(100);
myVector.push_back(200);
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
capacity()返回容量大小
也就是返回vector所能容纳的元素数量(在不重新分配内存的情况下)
容量不够,每次容量增加原容量的一半
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
cout << myVector.capacity() << "\n";//5
myVector.push_back(10);
//容量不够,每次容量增加原容量的一半 5 + 5/2 = 7
cout << myVector.capacity() << "\n";//7
myVector.push_back(10);
myVector.push_back(10);
cout << myVector.capacity() << "\n";//10
myVector.push_back(10);
myVector.push_back(10);
myVector.push_back(10);
cout << myVector.capacity() << "\n";//15
reserve()改变容量大小,只能变大,不能变小
设置变大后,容量就为设置的大小
设置变小后,容量还是之前的大小,不改变
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
cout << "容量大小:" << myVector.capacity() << "\n";//5
//cout << "元素个数:" << myVector.size() << "\n";//5
myVector.reserve(18);
cout << "reserve(18)后" << "\n";
cout << "容量大小:" << myVector.capacity() << "\n";//18
myVector.reserve(5);
cout << "reserve(5)后" << "\n";
cout << "容量大小:" << myVector.capacity() << "\n";//18
resize()使用,改变vector的元素个数
原型:
void resize (size_type n, value_type val = value_type());
第一个参数是修改元素的个数,
第二个参数,
如果修改的元素个数大于之前的元素个数,第二个参数才起作用,扩充的元素位置都为该元素,如果不写该参数,那么默认为0
如果修改的元素个数小于之前的元素个数,第二个参数不起作用
注:如果修改的元素个数大于之前的元素个数,容量大小也会变化为修改后的元素个数
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
cout << "容量大小:" << myVector.capacity() << "\n";//5
cout << "元素个数:" << myVector.size() << "\n";//5
cout << "====resize(3)元素个数变小后====" << "\n";
myVector.resize(3);//改变vector的大小并重新赋值
cout << "容量大小:" << myVector.capacity() << "\n";//7
cout << "元素个数:" << myVector.size() << "\n";//3
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
cout << "====resize(8)元素个数变大后====" << "\n";
myVector.resize(8);//改变vector的大小并重新赋值
//这里容量大小,是跟随resize的大小变化
cout << "容量大小:" << myVector.capacity() << "\n";//15
cout << "元素个数:" << myVector.size() << "\n";//11
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
empty(),判断元素是否为空
clear(),清空元素
vector<int> myVector(5, 0);//5个元素,每个元素为0
//给myVector每个下标赋值
for (int i = 0; i < 5; i++)
{
myVector.at(i) = i;
}
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n";
//判断元素是否为空
cout << boolalpha << myVector.empty() << "\n";
//清空元素
myVector.clear();
cout << boolalpha << myVector.empty() << "\n";
insert插入
vector<int> myVector;//5个元素,每个元素为0
myVector.push_back(4);
myVector.push_back(5);
myVector.push_back(6);
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
cout << "insert(ite, 3);后" << "\n";
ite = myVector.begin();
myVector.insert(ite, 3);//插入元素3
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
cout << "insert(myVector.end(), 2, 0);后" << "\n";
myVector.insert(myVector.end(), 2, 0);//插入2个66元素
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
cout << "insert(myVector.begin(), vectorInsert.begin(), vectorInsert.end());后" << "\n";
vector<int> vectorInsert;
vectorInsert.push_back(0);
vectorInsert.push_back(1);
vectorInsert.push_back(2);
myVector.insert(myVector.begin(), vectorInsert.begin(), vectorInsert.end());
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
erase() 删除指定位置或某个区间(前闭后开)的元素
vector<int> myVector;//5个元素,每个元素为0
myVector.push_back(4);
myVector.push_back(5);
myVector.push_back(6);
myVector.push_back(7);
myVector.push_back(8);
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
ite = myVector.end();
ite--;
myVector.erase(ite);//删除ite位置的元素
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
ite = myVector.end();
ite -= 2;
//返回值是指向删除的最后一个元素的下一个位置的迭代器
vector<int>::iterator tmp = myVector.erase(myVector.begin(), ite);//删除指定区间的元素
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
cout << "查看删除元素后的vector的首元素为:" << *tmp << "\n";
vector<int> myVector;//5个元素,每个元素为0
myVector.push_back(4);
myVector.push_back(5);
myVector.push_back(6);
vector<int> myVector2(2, 0);
//遍历vector
vector<int>::iterator ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
//遍历vector
ite = myVector2.begin();
for (ite; ite != myVector2.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
//交换两个vector
myVector.swap(myVector2);
//遍历vector
ite = myVector.begin();
for (ite; ite != myVector.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";
//遍历vector
ite = myVector2.begin();
for (ite; ite != myVector2.end(); ite++)
{
cout << *ite << '\t';
}
cout << "\n\n";