参考https://www.cnblogs.com/Nonono-nw/p/3462183.html
一、vector 的初始化:可以有五种方式,举例说明如下
(1) vectora(10); //定义了10个整型元素的向量(尖括号中为元素类型名,它可以是任何合法的数据类型),但没有给出初值,其值是不确定的。 (2)vector a(10,1); //定义了10个整型元素的向量,且给出每个元素的初值为1 (3)vector a(b); //用b向量来创建a向量,整体复制性赋值(随后当b中值改变,a中的值不改变) (4)vector a(b.begin(),b.begin()+3); //定义了a值为b中第0个到第2个(共3个)元素 (5)int b[7]={1,2,3,4,5,9,8}; vector a(b,b+7); //从数组中获得初值
测试代码:
#include
#include
using namespace std;
int main()
{
vector v1(10,2), v2(8,8), v3(v1), v4(v2),v5(v1.begin(),v1.begin()+4);
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
vector v6(a + 4, a + 6);
for (int i = 0; i < v1.size(); i++){
cout << v1[i];
}
cout << endl;
cout << "----------v1-----------" << endl;
for (int i = 0; i < v2.size(); i++){
cout << v2[i];
}
cout << endl;
cout << "----------v2-----------" << endl;
for (int i = 0; i < v3.size(); i++){
cout << v3[i];
}
cout << endl;
cout << "------v1----v3-----------" << endl;
for (int i = 0; i < v4.size(); i++){
cout << v4[i];
}
cout << endl;
cout << "-------v2----v4----------" << endl;
for (int i = 0; i < v5.size(); i++){
cout << v5[i];
}
cout << endl;
cout << "------v1 0 1 2 3------v5---------" << endl;
for (int i = 0; i < v6.size(); i++){
cout << v6[i];
}
cout << endl;
cout << "------------v6---------" << endl;
v1[0] = 0;
v1[1] = 1;
v1[3] = 3;
for (int i = 0; i < v1.size(); i++){
cout << v1[i];
}
cout << endl;
cout << "----------v1-change----------" << endl;
for (int i = 0; i < v3.size(); i++){
cout << v3[i];
}
cout << endl;
cout << "----------v3---No change--------" << endl;
for (int i = 0; i < v5.size(); i++){
cout << v5[i];
}
cout << endl;
cout << "------------v5---No change------" << endl;
system("pause");
return 0;
}
结果:
二、vector对象的几个重要操作,举例说明如下:(里面包括cout输出bool格式与int格式转换,代码最后)
#include
#include
using namespace std;
int main()
{
vector v1(10,2), v2(8,8), v3(v1), v4(v2),v5(v1.begin(),v1.begin()+4);
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
v1[0] = 0;
v1[1] = 1;
v1[3] = 3;
v1[8] = 8;
v1[9] = 120;
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl; //0 1 2 3 2 2 2 2 8 120
cout << v1.front() << " " << v1.back() << endl; //v1.front();返回v1的第一个元素 v1.back(); 返回v1的最后一个元素 0 120
v1.pop_back(); // //删除a向量的最后一个元素 120
cout << v1.front() << " " << v1.back() << endl; //0 8
v1.erase(v1.begin() + 1, v1.begin() + 3); //删除第一个元素后面,第三个元素后面的前面的值
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl; //结果 0 (1 2) 3 2 2 2 2 8
v1.push_back(111); //在v1的最后一个向量后插入一个元素,其值为111
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl; //结果 0 3 2 2 2 2 8 111
v1.insert(v1.begin() + 1, 5); //在v1的第1个元素后面的位置插入数值5,如v1为0 3 2 2 2 2 8 111,插入元素后为0 5 3 2 2 2 2 8 111
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl;
v1.insert(v1.begin() + 1, 3, 4); //在v1的第1个元素后面的位置插入3个数值4,如v1为0 5 3 2 2 2 2 8 111,插入元素后为0 4 4 4 5 3 2 2 2 2 8 111
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl;
int b[5] = { 11, 12, 13, 14, 15 };
v1.insert(v1.begin() + 1, b+1, b+3); //在v1的第1个元素后面的位置插入 b第一个元素后面,第三个元素后面的前面的值(12 13),如v1为0 4 4 4 5 3 2 2 2 2 8 111,插入元素后为0 12 13 4 4 4 5 3 2 2 2 2 8 111
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl;
vector v6(2,'Z');
v6[0] = 'A';
char bb[5] = { 'a', 'b', 'c', 'd', 'e' };
v6.insert(v6.begin() + 1, bb + 1, bb + 3);
for (int i = 0; i < v6.size(); i++){
cout << v6[i] << endl;
}
cout << "----------v6-change----------" << endl; //A b c Z(char型插入)
cout << v1.size() << " " << v6.size() << endl; //a.size(); 返回a中元素的个数;
v1.resize(10); //将v1的现有元素个数调至10个,多则删,少则补,其值随机 初始值 0 12 13 4 4 4 5 3 2 2 2 2 8 111
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl; //结果 0 12 13 4 4 4 5 3 2 2
v1.resize(12,6); //将v1的现有元素个数调至12个,多则删,少则补,补值6 初始值 0 12 13 4 4 4 5 3 2 2
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-change----------" << endl; //结果 0 12 13 4 4 4 5 3 2 2 6 6
v2.swap(v1); //v2为向量,将v1中的元素和v2中的元素进行整体性交换
for (int i = 0; i < v1.size(); i++){
cout << v1[i] << endl;
}
cout << "----------v1-v2-huhuan----v1-----" << endl; //8 8 8 8 8 8 8 8
for (int i = 0; i < v2.size(); i++){
cout << v2[i] << endl;
}
cout << "----------v1-v2-huhuan---v2------" << endl; //0 12 13 4 4 4 5 3 2 2 6 6
bool flag14 = (v1 == v4);
int flag24 = (v2 == v4); //向量的比较操作还有!=,>=,<=,>,<
cout << flag14 << endl; //通常,cout在显示布尔值之前将它转化为int 1
//cout << boolalpha << flag14 << endl; //cout显示布尔值 true
cout << "----------1111111111111------" << endl;
cout << flag24 << endl; //0
//cout << boolalpha << flag24 << endl; //cout显示布尔值 但flag24是int 所以依然输出0
cout << "---------00000000000000-----" << endl;
bool flag = v1.empty(); //判断v1是否为空,空则返回ture,不空则返回false
cout << flag << endl; //通常默认情况下,cout在显示布尔值之前将它转化为int 0
cout << boolalpha << flag << endl; //cout显示布尔值
cout << flag << endl; //沿袭上一种返回方式
cout << noboolalpha << flag << endl; //bool到int
cout << flag << endl;
cout << "--------------------" << endl;
v1.clear(); //清空v1中的元素
bool flag1 = v1.empty();
cout << flag1 << endl;
system("pause");
return 0;
}
三 几种重要的算法,使用时需要包含头文件:
#include
1 sort(v6.begin(), v6.end()); // 排序
2 reverse(v6.begin(), v6.begin()+3); // 元素倒置
3 int num =count(v6.begin(), v6.end(), 'Z'); //在v6中的从v6.begin()到v6.end()的元素中查找'Z'的个数
代码测试如下
#include
#include
#include
using namespace std;
int main()
{
vector v6(2,'Z');
v6[0] = 'A';
char bb[5] = { 'a', 'b', 'c', 'd', 'e' };
v6.insert(v6.begin() + 1, bb + 1, bb + 3);
for (int i = 0; i < v6.size(); i++){
cout << v6[i] << endl;
}
cout << "----------v6-change----------" << endl; //A b c Z(char型插入)
sort(v6.begin(), v6.end()); // 排序
for (int i = 0; i < v6.size(); i++){
cout << v6[i] << endl;
}
cout << "----------v6-change----------" << endl; //A Z b c
reverse(v6.begin(), v6.begin()+3); // 元素倒置
for (int i = 0; i < v6.size(); i++){
cout << v6[i] << endl;
}
cout << "----------v6-change----------" << endl; //b Z A c
v6.push_back('Z'); //末尾加元素
for (int i = 0; i < v6.size(); i++){
cout << v6[i] << endl;
}
cout << "----------v6-change----------" << endl; //b Z A c Z
int num =count(v6.begin(), v6.end(), 'Z'); //在v6中的从v6.begin()到v6.end()的元素中查找'Z'的个数
cout <