Member functions
//部分代码
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// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );
(destructor) ~vector()
operator= vector& operator= (const vector& x);
Iterators:
Capacity:
size Return size
resize resize (size_type n, value_type val = value_type());
如果n小于现有的储存空间,则缩小; 如果n大于现有的储存空间,则增加, 如果val有值,则没有初始化的空间由val初始化; 如果val没有传入,则由value初始化。 |
capacity Return size of allocated storage capacity
empty Test whether vector is empty
reserve Request a change in capacity 会扩大不会缩小
shrink_to_fit Shrink to fit 使size=capacity
Element access:
data 返回指向容器第一个元素的指针
Modifiers:
//部分代码
first.assign (7,100); // 7 ints with a value of 100std::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.左闭右开
erase//注意迭代器失效
//部分代码
std::vector<int> myvector = {10,20,30};
auto it = myvector.emplace ( myvector.begin()+1, 100 );
//pos,val,return pos/分配失败抛出bad_alloc
myvector.emplace ( it, 200 );
myvector.emplace ( myvector.end(), 300 );
emplace_back void emplace_back (Args&&... args);
Allocator:
//部分代码
std::vector<int> myvector;
int * p;
unsigned int i;
// allocate an array with space for 5 elements using vector's allocator:
p = myvector.get_allocator().allocate(5);
// construct values in-place on the array:
for (i=0; i<5; i++) myvector.get_allocator().construct(&p[i],i);
std::cout << "The allocated array contains:";
for (i=0; i<5; i++) std::cout << ' ' << p[i]; std::cout << '\n';
// destroy and deallocate:
for (i=0; i<5; i++) myvector.get_allocator().destroy(&p[i]); myvector.get_allocator().deallocate(p,5);
Non-member function overloads
relational operators 依次比较值的大小
Template specializations
vector<bool> 每个值都存储在一个位中