STL vector的使用(一)基础

一. vector介绍:

    vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector之所以被认为是一个容器,是因为它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。

二. 使用介绍:

1. 为了可以使用vector,必须在你的头文件中包含下面的代码:

#include <vector>
2. vector属于std命名域的,因此需要通过命名限定,如下完成你的代码:

using std::vector;
vector<int> vInts;
或者连在一起,使用全名:

std::vector<int> vInts;

三. Vector成员函数(Vector Member Functions):

Function
Description
assignErases a vector and copies the specified elements to the empty vector.
1. c.assign(beg,end):[beg; end)区间中的数据赋值给c
2. c.assign(n,elem):将n个elem的拷贝赋值给c。
atReturns a reference to the element at a specified location in the vector.
1. c.at(idx):传回索引idx所指的数据,如果idx越界,抛出out_of_range。
backReturns a reference to the last element of the vector.
传回最后一个数据,不检查这个数据是否存在。
begin

Returns a random-access iterator to the first element in the container.
传回迭代器的第一个数据。
capacityReturns the number of elements that the vector could contain without allocating more storage.
返回容器中数据个数。
clearErases the elements of the vector.
移除容器中所有数据。
emptyTests if the vector container is empty.
判断容器是否为空。
endReturns a random-access iterator that points just beyond the end of thevector.
指向迭代器中的最后一个数据地址。
eraseRemoves an element or a range of elements in a vector from specified positions.
1. c.erase(pos):  删除pos位置的数据,传回下一个数据的位置。
2. c.erase(beg,end):删除[beg,end)区间的数据,传回下一个数据的位置。
frontReturns a reference to the first element in a vector.
传回第一个数据。
get_allocatorReturns an object to the allocator class used by a vector.
使用构造函数返回一个拷贝。
insertInserts an element or a number of elements into the vector at a specified position.
1. c.insert(pos,elem):在pos位置插入一个elem拷贝,传回新数据位置。
2. c.insert(pos,n,elem):在pos位置插入n个elem数据。无返回值。
3. c.insert(pos,beg,end):在pos位置插入在[beg,end)区间的数据。无返回值。
max_sizeReturns the maximum length of the vector.
返回容器中最大数据的数量。
pop_backDeletes the element at the end of the vector.
删除最后一个数据。
push_backAdds an element to the end of the vector.
在尾部加入一个数据
rbeginReturns an iterator to the first element in a reversed vector.
传回一个逆向队列的第一个数据。
rend
Returns an iterator to the end of a reversed vector.
传回一个逆向队列的最后一个数据的下一个位置。
resizeSpecifies a new size for a vector.
重新指定队列的长度。
reserveReserves a minimum length of storage for a vector object.
保留适当的容量。
sizeReturns the number of elements in the vector.
返回容器中实际数据的个数。
swapExchanges the elements of two vectors.
1. c1.swap(c2) 
2. swap(c1,c2)
都是将c1和c2元素互换。
vectorConstructs a vector of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other vector.


四. Vector操作符(Vector Operators):

Operator
Description
operator[]Returns a reference to the vector element at a specified position.

五. 构建Vector(Constructing a Vector)

1. Construct an empty vector to hold objects of type Widget

vector<Widget> vWidgets;
//     ------
//      |
//      |- Since vector is a container, its member functions
//         operate on iterators and the container itself so 
//         it can hold objects of any type.

2.  Construct a vector to hold 500 Widgets:

vector<Widget> vWidgets(500);

3.  Construct a vector to hold 500 Widgets initialized to 0:

vector<Widget> vWidgets(500, Widget(0));

4.  Construct a vector of Widgets from another vector of Widgets:

vector<Widget> vWidgetsFromAnother(vWidgets);


六. 向vector添加数据:

for(int i= 0;i<10; i++)
    vWidgets.push_back(Widget(i));


七. 获取元素的个数:

很多时候我们不必要知道vector里面有多少数据,vector里面的数据是动态分配的,使用push_back()的一系列分配空间常常决定于文件或一些数据源。如果你想知道vector存放了多少数据,你可以使用empty()。获取vector的大小,可以使用size()。例如,如果你想获取一个vector v的大小,但不知道它是否为空,或者已经包含了数据,如果为空想设置为-1,你可以使用下面的代码实现:

int nSize = v.empty() ? -1 : static_cast<int>(v.size());


八. 访问vector中的数据

1vector::at()
2vector::operator[]

vector<int> v;
v.reserve(10);


for(int i=0; i<7; i++)
    v.push_back(i);


try
{
 int iVal1 = v[7];  // not bounds checked - will not throw
 int iVal2 = v.at(7); // bounds checked - will throw if out of range
}
catch(const exception& e)
{
 cout << e.what();
}




  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值