c++顺序容器之vector

“通常,使用vector是最好的选择,除非你有很好的理由选择其他容器” ——【摘自C++ primer第5版】


迭代器:

与前两篇中讲的array,string类型一样,vector也有如下几种获取迭代器的成员函数:

/***************************************************
"与容器一样,迭代器有着公共的接口,如果一个迭代器提供某个操作,那么所有提供相同操作的迭代器对这个操作的实现方式都是相同的"——【摘自C++ primer第5版】
***************************************************/

begin();    end();    rbegin();    rend();    cbegin();     cend();    crbegin();    crend();

// r代表reverse     c代表const


有关容器大小:

size();  Returns the number of elements in the vector.  This is the number of actual objects held in the vector, which is not necessarily equal to its                  storage capacity.

max_size();  Returns the maximum number of elements that the vector can hold.  This is the maximum potential size the container can reach due                            to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it                            can still fail to allocate storage at any point before that size is reached.

                    /***************返回类型是size_t或者unsigned int,不能是int******************************/

capacity();  Returns the size of the storage space currently allocated for the vector

                  /********************若容器大小不超过capacity,则不用重新分配内存**************************/

empty();  Returns whether the vector is empty (i.e. whether its size is 0).

resize();  Resizes the container so that it contains n elements.

             void resize (size_type n);
             void resize (size_type n, const value_type& val);
reserve();  Request a change in capacity.Requests that the vector capacity be at least enough to contain n elements.If n is greater than the c                         urrent vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater).
                        In all other cases, the function call does not cause a reallocation and the vector capacity is not affected.This function has no effect                          on the vector size and cannot alter its elements.
                        void reserve (size_type n);
shrink_to_fit();  Requests the container to reduce its capacity to fit its size.The request is non-binding, and the container implementation is free                               to optimize otherwise and leave the vector with a capacity greater than its size.This may cause a reallocation, but has no 
                               effect on the vector size and cannot alter its elements.
                    void shrink_to_fit();

 
vector<int> vec1;
vector<int> vec2(100);
cout<<"capacity is "<<vec1.capacity()<<endl;
cout<<"capacity is "<<vec2.capacity()<<endl;
程序执行结果: 
/********************************************
从结果可以看出,vector的capacity与array比较相似,区别于string的capacity.
********************************************/

 
容器内元素操作:
vector 也有[]下标操作;
//以下4个函数意义与上一篇string的同名成员函数一样
at();
front();
back();
data();
vector<int> vec;
int i;
for(i=1;i<6;++i)
	vec.push_back(i);
cout<<vec.front()<<endl;
cout<<vec.back()<<endl;
cout<<*(vec.data())<<endl;


 
修改容器元素:
assign();   assign(n,v); 给容器分配n个元素,值均为n,执行完之后容器size==n.
push_back(ElementType v); 向容器尾部压入值为v的元素,size++;
pop_back(); 删除容器尾部最后一个元素,size--;
void swap(vector& vec); 交换两个vector的内容;
clear(); 清空,size==0;
iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last); 删除[first,last)范围内的元素,size亦同步变化;

插入操作参考上篇string成员函数
iterator insert (const_iterator position, const value_type& val);
iterator insert (const_iterator position, size_type n, const value_type& val);
iterator insert (const_iterator position, InputIterator first, InputIterator last);
iterator insert (const_iterator position, value_type&& val);
iterator insert (const_iterator position, initializer_list<value_type> il); //vs2012+支持initializer_list
 
iterator emplace(const_iterator pos,ElementType v); 在容器pos位置处添加元素v,容器size同步+1;
void emplace_back(ElementType v); 在容器尾部添加元素v,容器size同步+1;
/*****************************
注意区别emplace_back()与push_back(),关键在于是否会改变容器的size
********************************/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值