vector简介
vector类似C++内置数组,其元素在内存中连续存放,可以通过迭代器和下标进行随机访问,vector被实现为动态数组,还可以方便的实现自动增长[push_back()/insert()...]和自动缩小[resize()...]等
vector的优点
1.可以通过[]/at()来实现随机访问元素
2.可以通过迭代器以多种顺序来遍历各元素
3.可以高效的通过push_back()/pop_back()在其尾端添加或移除元素
vector4个构造函数
1.初始化为空的vector
2.初始化为n个相同值拷贝的vector
3.初始化为一对迭代器之间值的vector[first,last)
4.初始化为用另一个vector而拷贝的vector[拷贝构造函数]
原型如下:
1.explicit vector ( const Allocator& = Allocator() );
2.explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
3.template <class InputIterator>
vector ( InputIterator first, InputIterator last, const Allocator& = Allocator() );
4.vector ( const vector<T,Allocator>& x );
eg:
vector<int> first; // empty vector of ints
vector<int> second (4,100); // four ints with value 100
vector<int> second (4); //four ints with value 0
vector<int> third (second.begin(),second.end()); // iterating through second
vector<int> fourth (third); // a copy of third
// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
Capacity:
empty | Test whether vector is empty |
size | Return size |
max_size | Return maximum size [test winxp 32bit vs2008 value is: 1073741823] |
resize | Change size |
capacity | Return size of allocated storage capacity→vector unique |
reserve | Request a change in capacity→vector unique |
eg:
reserve//改变capacity大小
vector<string> content;
size_t filesize;
ifstream file ("test.bin",ios::in|ios::ate|ios::binary);
if (file.is_open())
{
filesize=file.tellg();
content.reserve(filesize);//改变capacity为文件内容大小
//...other process
}
resize//改变vector的大小
vector<int> myvector;
unsigned int i;
// set some initial content:
for (i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);
myvector.resize(8,100);
myvector.resize(12);
cout << "myvector contains:";
for (i=0;i<myvector.size();i++)
cout << " " << myvector[i];
Output:
1 2 3 4 5 100 100 100 0 0 0 0
1 2 3 4 5 6 7 8 9 10 11 12//对应的序号
Element access:
operator[] | Access element |
at | Access element |
front | Access first element |
back | Access last element |
eg:
front
vector<int> myvector;
myvector.push_back(78);
myvector.push_back(16);
// now front equals 78, and back 16
myvector.front() -= myvector.back();
cout << "myvector.front() is now " << myvector.front() << endl;
Output:
62
back
vector<int> myvector;
myvector.push_back(10);
while (myvector.back() != 0)
{
myvector.push_back ( myvector.back() -1 );
}
cout << "myvector contains:";
for (unsigned i=0; i<myvector.size() ; i++)
cout << " " << myvector[i];
Output:
10 9 8 7 6 5 4 3 2 1 0
Modifiers:
assign | Assign vector content |
push_back | Add element at the end |
pop_back | Delete last element |
insert | Insert elements |
erase | Erase elements |
swap | Swap content//algorithm exists swap, and the same behavior. |
clear | Clear content |
assign
//assign不能指定位置只能从begin()处开始赋值,且相当于之前先执行了clear()清空了vector
1.使用另一输入迭代器[first,last)赋值
2.使用n个相同的值u赋值
原型如下:
1.template <class InputIterator>
void assign ( InputIterator first, InputIterator last );
2.void assign ( size_type n, const T& u );//repetition n times of copies of element u
eg:
vector<int> first;
vector<int> second;
vector<int> third;
first.assign (7,100); // a repetition 7 times of value 100
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.
insert
1.把x插入到迭代器position,返回新插入元素的迭代器
2.把n个相同的值x插入到迭代器position
3.在迭代器position插入另一输入迭代器[first,last)
原型如下:
1.iterator insert ( iterator position, const T& x );
2.void insert ( iterator position, size_type n, const T& x );
3.template <class InputIterator>
void insert ( iterator position, InputIterator first, InputIterator last );
eg:
insert
vector<int> myvector (3,100);
vector<int>::iterator it;
it = myvector.begin();
it = myvector.insert ( it , 200 );
myvector.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector.begin();
vector<int> anothervector (2,400);
myvector.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector.insert (myvector.begin(), myarray, myarray+3);
for (it=myvector.begin(); it<myvector.end(); it++)
cout << " " << *it;
Output:
501 502 503 300 300 400 400 200 100 100 100
erase
1.删除迭代器position上的元素,返回删除元素之后紧随的有效迭代器,如果删除的是iVec.erase(end()-1),则后面为无效迭代器,解引用会出错
2.删除迭代器之间的元素[first,last),返回删除元素之后紧随的有效迭代器,如果删除的是iVec.erase(iVec.begin()+1,iVec.end()),同理上面
总之在erase的时候要非常小心处理迭代器失效问题
原型如下:
1.iterator erase ( iterator position );
2.iterator erase ( iterator first, iterator last );
eg:
erase
unsigned int i;
vector<unsigned int> myvector;
// set some values (from 1 to 10)
for (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);
cout << "myvector contains:";
for (i=0; i<myvector.size(); i++)
cout << " " << myvector[i];
Output:
myvector contains: 4 5 7 8 9 10
eg:
swap
vector<int> first (3,100); // three ints with a value of 100
vector<int> second (5,200); // five ints with a value of 200
first.swap(second);
Allocator:
get_allocator
eg:
vector<int> iVec;
int *p;
p = iVec.get_allocator().allocate(5);
for (size_t i = 0; i != 5; ++i)
{
p[i] = i;
}
for (size_t i=0; i<5; i++) cout << " " << p[i];
cout << endl;
iVec.get_allocator().deallocate(p,5);
Output:
0 1 2 3 4