1、std::vector的特点
是一个模板类,提供了动态数组的通用功能,使用时要包含头文件
#include <vector>
- 在数组末尾添加元素所需的时间是固定的,即在末尾插入元素的所需时间不随数组大小而异,在末尾删除元素也如此;
- 在数组中间添加或删除元素所需的时间与该元素后面的元素个数成正比;
- 存储的元素数是动态的,而vector类负责管理内存。
2、典型的vector操作
(1)实例化vector:
#include "stdafx.h"
#include <vector>
int main()
{
std::vector<int> vecDynamicIntegerArray;
//为整型具体化vector类,即实例化一个存储整型数据的vector,
//它使用了默认构造函数,在不知道要存储多少个整数是,构造函数很有用
//Instantiate a vector with 10 elements(it can grow larger)
std::vector<int> vecArrayWithTenElements(10);
//初始化方式一
//Instantiate a vector with 10 elements,each initialized to 90
//初始化方式二
std::vector<int> vecArrayWithTenInitializedElements(10, 90);
//使用一个vector实例化另一个vector
std::vector<int> vecSomeElementsCopied(vecArrayWithTenElements.begin(), vecArrayWithTenElements.begin() + 5);
return 0;
}
(2)在vector中插入元素
push_back:在数组末尾插入元素
#include "stdafx.h"
#include <vector>
#include <iostream>
int main()
{
std::vector<int> vecDynamicIntegerArray;
//Insert sample integers into the vector:
vecDynamicIntegerArray.push_back(50);
vecDynamicIntegerArray.push_back(1);
vecDynamicIntegerArray.push_back(987);
vecDynamicIntegerArray.push_back(1001);
std::cout << "The vector contains ";
std::cout << vecDynamicIntegerArray.size() << " Elements";
return 0;
}
使用数组语法设置vector中的值
#include "stdafx.h"
#include <vector>
#include <iostream>
int main()
{
std::vector<int> vecDynamicIntegerArray(4);
//Copy integer values into individual element locations
vecDynamicIntegerArray[0] = 50;
vecDynamicIntegerArray[1] = 1;
vecDynamicIntegerArray[2] = 987;
vecDynamicIntegerArray[3] = 1001;
std::cout << "The vector contains ";
std::cout << vecDynamicIntegerArray.size() << " Elements";
getchar();
return 0;
}
使用vector::insert在中间插入元素
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//Instantiate a vector with 4 elements,each initialized to 90
vector <int> vecIntegers(4, 90);
cout << "The initial contants of the vector are:";
vector<int>::iterator iElement;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在开始处插入25
vecIntegers.insert(vecIntegers.begin(), 25);
cout << "The vector after inserting an element at the beginning: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在末尾插入两个45
vecIntegers.insert(vecIntegers.end(), 2, 45);
cout << "The vector after inserting two elements at the end: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//初始化一个包含两个元素,值为30的向量
vector <int> vecAnother(2, 30);
//从另一个容器插入两个元素在第一个元素的位置
vecIntegers.insert(vecIntegers.begin() + 1, vecAnother.begin(), vecAnother.end());
cout << "The vector after inserting contents from another ";
cout << "in the middle:" << endl;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
getchar();
return 0;
}// vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//Instantiate a vector with 4 elements,each initialized to 90
vector <int> vecIntegers(4, 90);
cout << "The initial contants of the vector are:";
vector<int>::iterator iElement;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在开始处插入25
vecIntegers.insert(vecIntegers.begin(), 25);
cout << "The vector after inserting an element at the beginning: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在末尾插入两个45
vecIntegers.insert(vecIntegers.end(), 2, 45);
cout << "The vector after inserting two elements at the end: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//初始化一个包含两个元素,值为30的向量
vector <int> vecAnother(2, 30);
//从另一个容器插入两个元素在第一个元素的位置
vecIntegers.insert(vecIntegers.begin() + 1, vecAnother.begin(), vecAnother.end());
cout << "The vector after inserting contents from another ";
cout << "in the middle:" << endl;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
getchar();
return 0;
}// vector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <vector>
#include <iostream>
using namespace std;
int main()
{
//Instantiate a vector with 4 elements,each initialized to 90
vector <int> vecIntegers(4, 90);
cout << "The initial contants of the vector are:";
vector<int>::iterator iElement;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在开始处插入25
vecIntegers.insert(vecIntegers.begin(), 25);
cout << "The vector after inserting an element at the beginning: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//在末尾插入两个45
vecIntegers.insert(vecIntegers.end(), 2, 45);
cout << "The vector after inserting two elements at the end: ";
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
cout << endl;
//初始化一个包含两个元素,值为30的向量
vector <int> vecAnother(2, 30);
//从另一个容器插入两个元素在第一个元素的位置
vecIntegers.insert(vecIntegers.begin() + 1, vecAnother.begin(), vecAnother.end());
cout << "The vector after inserting contents from another ";
cout << "in the middle:" << endl;
for (iElement = vecIntegers.begin(); iElement != vecIntegers.end(); ++iElement)
{
cout << *iElement << ' ';
}
getchar();
return 0;
}