vector是C++标准模板库,它是一个多功能、能够操作多种数据结构和算法的模板类。vector是一个容器,能够像容器一样存放各种类型的对象。vector是一个能够存放任意类型的动态数组,能够增加和删除元素,可以完全替代C语言风格的动态数组,由于vector能够自动的管理内存,我们不需要考虑内存的增长和释放、具有很好的安全性。
//定义具有10个整型元素的向量
//(尖括号为元素类型名,它可以是任何合法的数据类型),
//不具有初值,其值不确定
vectora(10);
//定义具有10个整型元素的向量,且给出的每个元素初值为1
vectora(10,1);
//用向量b给向量a赋值,a的值完全等价于b的值
vectora(b);
//将向量b中从0-2(共三个)的元素赋值给a,a的类型为int型
vectora(b.begin(),b.begin+3);
//从数组中获得初值
int b[7]={1,2,3,4,5,6,7};
vector a(b,b+7);
#include
#include
using namespace std;
int main ()
{
// vector::begin/end
std::vector<int> myvector;
for (int i=1; i<=5; i++) myvector.push_back(i*2); //generate a vector[1,2,3,4,5]
std::cout<<"size of vector is:\n"<<myvector.size()<<endl;
std::cout<<"back of vector is:\n" <<myvector.back()<<endl;
vector<int>::iterator v = myvector.begin()+1;//迭代器指向myvector的第二个元素
std::cout<<"begin of vector is:\n" <<*v <<endl;//打印这个指针变量
for (std::vector<int>::iterator it = myvector.begin() ; it < myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
output is:
size of vector is:
5
back of vector is:
10
begin of vector is:
4
2 4 6 8 10
std::vector<int> first;
std::vector<int> second;
std::vector<int> third;
first.assign (7,100); // 7 ints with a value of 100
std::vector<int>::iterator it;
it=first.begin()+1;
second.assign (it,first.end()-1); // the 5 central values of first
int myints[] = {1776,7,4};
third.assign (myints,myints+3); // assigning from array.from 1776 to *1776 +3, three elements
std::cout << "Size of first: " << int (first.size()) << '\n';
for (std::vector<int>::iterator itf = first.begin() ; itf < first.end(); itf++)
{std::cout <<" "<<*itf;}
std::cout<<endl;
std::cout << "Size of second: " << int (second.size()) << '\n';
for (std::vector<int>::iterator its = second.begin() ; its < second.end(); its++)
{std::cout <<" "<<*its;}
std::cout<<endl;
std::cout << "Size of third: " << int (third.size()) << '\n';
for (std::vector<int>::iterator itt = third.begin() ; itt < third.end(); itt++)
{std::cout <<" "<<*itt;}
std::cout<<endl;
return 0;
output is:
Size of first: 7
100 100 100 100 100 100 100
Size of second: 5
100 100 100 100 100
Size of third: 3
1776 7 4
at 和 operator[]具有一样的access elements 作用,但是at能够抛出out of range exception,所以推荐使用at,而不是运算符 [ ]
std::vector<int> myvector (4); // 10 zero-initialized ints
// assign some values:
// or access some element;
for (unsigned i=0; i<myvector.size(); i++)
myvector.at(i)=i*i;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); i++)
std::cout << ' ' << myvector.at(i);
std::cout << '\n';
return 0;
output is:
myvector contains: 0 1 4 9
return the last element
myvector.back()
size就是当前的vector长度,capacity是容器长度,一定是大于等于这个size的,当然当vector增长到capacity长度时不是说就塞不下了,capacity会继续allocate增大分配内存空间,而可以增大到的最大的理论极限就是max_size.
std::vector<int> myvector;
// set some content in the vector:
for (int i=0; i<100; i++) myvector.push_back(i);
std::cout << "size: " << (int) myvector.size() << '\n';
std::cout << "capacity: " << (int) myvector.capacity() << '\n';
std::cout << "max_size: " << (int) myvector.max_size() << '\n';
return 0;
size: 100
capacity: 128
max_size: 1073741823
用于访问vector首尾元素,但无法修改该值
clear vector content
myvector.clear();
指针指向vector首位
std::vector<int> myvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100;
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
output is:
myvector contains: 10 20 0 100 0
// Returns whether the vector is empty
// (i.e. whether its size is 0).
myvector.empty()
std::vector<int> myvector;
// set some values (from 1 to 10)
for (int i=1; i<=10; i++) myvector.push_back(i);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector.size(); ++i)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
output is:
myvector contains: 4 5 7 8 9 10
// return the first and the last elements.
//unlike vector::begin/end, front/back returns direct reference.
myvector.front()
myvector.back()
//在a的第一个元素(从第0个算起)位置插入数值5,
a.insert(a.begin()+1,5);
//在a的第一个元素(从第0个算起)位置插入3个数,其值都为5
a.insert(a.begin()+1,3,5);
//b为数组,在a的第一个元素(从第0个元素算起)的位置插入b的第三个元素到第5个元素(不包括b+6)
a.insert(a.begin()+1,b+3,b+6);
std::vector<int> myvector (3,100);//[100,100,100]
std::vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );[200,100,100,100]
myvector.insert (it,2,300);[300,300,200,100,100,100]
// "it" no longer valid, get a new one:
it = myvector.begin();
std::vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
//[300,300,400,400,200,100,100,1000]
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
//[501,502,503,300,300,400,400,200,100,100,100]
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
output is:
myvector contains: 501 502 503 300 300 400 400 200 100 100 100
delete and add element at the end
//increase vector capacity to n
myvector::reserve(n)
std::vector<int> myvector;
// set some initial content:
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);//[1,2,3,4,5]
myvector.resize(8,100);//[1,2,3,4,5,100,100,100]
myvector.resize(12);//[1,2,3,4,5,100,100,100,0,0,0,0]
std::cout << "myvector contains:";
for (int i=0;i<myvector.size();i++)
std::cout << ' ' << myvector[i];
std::cout << '\n';
return 0;
output is:
myvector contains: 1 2 3 4 5 100 100 100 0 0 0 0
// shrink capacity to fit current vector size
myvector.shrink_to_fit()