vector类成员函数

Member functions

(constructor)

//部分代码

 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:

assign

//部分代码
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

erase//注意迭代器失效

swap

clear

emplace 

//部分代码

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:

get_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 依次比较值的大小

swap

Template specializations

vector<bool> 每个值都存储在一个位中

创作活动

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值