c plus STL and vector

本文详细介绍了STL(标准模板库),它是C++标准库的重要组成部分,包括容器、迭代器、分配器、适配器、算法和仿函数六大组件。重点讲解了向量(Vector)这一容器的特点、构造方法及成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

STL(Standard Template Library)

STL is a c plus software library that influenced many parts of the C++ Standard Library.It provides four components, which called algorithms, containers, functors, and iterators. Such as HP STL、PJ STL、 SGI STL etc.

STL has been built within your complier.And it has many different versions.http://write.blog.youkuaiyun.com/postedit

In ANSI C ++ STL is organized into 13 header files:<algorithm>, <deque>, <functional><iterator>, <vector>, <list>, <map>, <memory>, <numeric>, <queue>, <set>, <stack>, <utility>.

And STL can be devided into 6 parts, including containers, iterators, allocators, adaptors, algorithms, functors.


Vector

Vector is considered a container, because of it can storge variable types objects just like containers.it can be used as a dynamic array . In order to use vector, we should include the header file <vector>. And the vector is contained in the std namesapce,so if we intend to use the vector, we also need to limit the namespace as follows:

usingnamespace std;

Of course,for using vector, there are some advantages but also some disadvantages.It can work fast for random access..., while slowly for insertion and deletion...


Constructors:

vector();

vector(int initialcapacity, int capacityIncrement);

vector(int initialcapacity);

For the first constructor, operator will manage the vector automatically.but for the latter two constructors, vector will be initialized with the fixed capacity. and when the capacity is not enough to store the data, the vector' capasity will be increased with the size of  capacityincrement.


define:

vector<type> vector_name;

member functions:

assign(beg, end);

assign the data of the region between beg and end to c.


assgin(n, elem);

assigin n datas to c.


at(idx);

return the data of index idx,if cross the border, throw an exception :out_of_range.


back();

return the last data without check if the data exist.


begin();

return the firt address of the iterator.


capacity();

return the number of the datas that be stored in capacity.


clear();

remove all datas from the capacity.


empty();

check if the capacity is empty.


end();

refer to the next of the last data, which is not exist.


erase(pos);

delete the data at position pos,and return the address of the next data.


erase(beg, end);

delete datas of the region between beg and end, and return the address of the next data.


front();

return the first data.


get_allocator();

use the copy constructor and return a copy.


insert(pos, elem);

insert an element at the position pos, and return the address of the new data.


insert(pos, n, elem);

insert n datas at the position, and without return value.


max_size();

return the number of the maximum datas.


pop_back();

delete the last data.


push_back(elem);

insert an element to the end of the datas.


rbegin();

return the first data of an reverse queue.


rend();

refer to the next address of the last data in an reverse queue.


resize(num);

resize the length of the queue.


reserve();

reserve some capacitys.


size();

return the number of the datas in capacity.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值