template < class T, class Alloc = allocator<T> > class vector;//一般模板
构造函数:
std::vector<int> first; // empty vector of ints
std::vector<int> second (4,100); // four ints with value 100
std::vector<int> third (second.begin(),second.end()); // iterating through second
std::vector<int> fourth (third); // a copy of third
赋值运算符重载(operator=):vector之间可以直接赋值,数组不行。
std::vector<int> foo (3,0);
std::vector<int> bar (5,0);
bar = foo;
foo = std::vector<int>();
迭代器:
begin,end,rbegin,rend; //iterator迭代器
cbegin,cend,crbegin,crend; //const_iterator 可以读取元素,但是不能修改元素的值
std::vector<int>::iterator it = myvector.begin() ;
std::vector<int>::reverse_iterator rit = myvector.rbegin();
容量:
size, max_size(所有vector的最大容量),capacity(当前vector的最大容量)
resize(重新定义size,截断或者用值扩容),reserve(n)(n>capacity重新改变capacity大小,否则不变)
for (int i=1;i<10;i++) myvector.push_back(i);
myvector.resize(5);//截断数组长度使之为5,但是capacity不变
myvector.resize(8,100);
myvector.resize(12);
std::vector<int> bar;
sz = bar.capacity();
bar.reserve(100);//直接将capacity设置为100.
empty(空返回true),shrink_to_fit(容器将减小capacity到size大小)
std::vector<int> myvector (100);
std::cout << "1. capacity of myvector: " << myvector.capacity() << '\n';
myvector.resize(10);
std::cout << "2. capacity of myvector: " << myvector.capacity() << '\n';
myvector.shrink_to_fit();
std::cout << "3. capacity of myvector: " << myvector.capacity() << '\n';
1. capacity of myvector: 100 2. capacity of myvector: 100 3. capacity of myvector: 10
元素访问:
[] 操作符重载 与 at :跟访问数组一样。A[3] == A.at(3).
front与back方法:myvector.front()等同于myvector[0]
std::vector<int> myvector;
myvector.push_back(78);
myvector.push_back(16);
// now front equals 78, and back 16
myvector.front() -= myvector.back();
std::cout << "myvector.front() is now " << myvector.front() << '\n';
data方法:返回vector第一个元素的地址指针。
std::vector<int> myvector (5);
int* p = myvector.data();
*p = 10;
++p;
*p = 20;
p[2] = 100; // 10 20 0 100 0
修改:
assign(分配):
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.
push_back和pop_back(尾部插入与删除):
insert:在输入参数迭代器的位置上插入数值,然后返回值类型也是迭代器,值为插入元素的第一个位置。
std::vector<int> myvector (3,100);
std::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();
std::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);
std::cout << "myvector contains:";
for (it=myvector.begin(); it<myvector.end(); it++)
std::cout << ' ' << *it;
std::cout << '\n';
myvector contains: 501 502 503 300 300 400 400 200 100 100 100
erase(删除):删除vector中单个值(position)或者连续一串([first,last))
它删除给定位置(迭代器)的数据,然后返回的是指向被删除元素之后的元素的迭代器。
iterator erase (const_iterator position); iterator erase (const_iterator first, const_iterator last);
// erase the 6th element
myvector.erase (myvector.begin()+5);
// erase the first 3 elements:
myvector.erase (myvector.begin(),myvector.begin()+3);
swap:交换两个vector的值
A.swap(B);
clear:清空一个vector,使其size等于0.
A.clear();
emplace:相当于insert,但是它是对传入数据进行构造。emplace_back相当于push_back.
假如A里面都是自定义类对象(链表节点),此方法如下构造:
A.emplace(A.begin(), Node * root, val);