您的位置:首页 > 其它

STL中的vector源码剖析

2017-04-26 16:36 351 查看
先上源码,源码部分摘要:

/*
*STL 源码
*vector定义摘要
*/
template <class T, class Alloc = alloc>
class vector{
public:
//vector 的嵌套型别定义
typedef T           value_type;
typedef value_type* pointer;
typedef value_type* iterator;
typedef value_type* reference;
typedef size_t      size_type;
protected:
iterator start;
iterator finish;
iterator end_of_storage;
void fill_initialize(size_type n, const T& value) {
start = alloc_and_fill(n, value);
finish = start + n;
end_of_storage = finish;
}
public:
iterator begin(){ return start; }
iterator end(){ return finish; }
size_type size() const { return size_type(end() - begin()); }
bool empty() const { return begin() == end(); }
reference operator[](size_type n) { return *(begin() + n); }

vector() : start(0), finish(0), end_of_storage(0) {}  //未初始化时未申请空间
vector(size_type n, const T& value) { fill_initialize(n, value); }
vector(int n, const T& value) { fill_initialize(n, value); }
vector(long n, const T& value) { fill_initialize(n, value); }
explicit vector(size_type n) { fill_initialize(n, T()); }

~vector() {    //自动释放空间
destory(start, finish);
deallocate();
}
reference front() { return *begin(); }
reference back() { return *(end() - 1); }
void push_back(const T& x) {     //向vector末尾压入一个数据
if (finish != end_of_storage) {
construct(finish, x);
++finish;
}
else
insert_aux(end(), x);
}

void pop_back() {       弹出末尾数据
--finish;
destory(finish);
}

iterator erase(iterator position) {
if (position +1 != end())
copy(position + 1, finish, position);
--finish;
destory(finish);
return position;
}
void resize(size_type new_size, const T& x) {    //重置大小(空间并未释放)
if (new_size < size())
erase(begin() + new_size, end());
else
insert(end(), new_size - size(), x);
}
void resize(size_type new_size) { resize(new_size, T()); }
void clear() { erase(begin(), end());            //清零,重置大小为0,空间并未释放

}


vector维护的是一个连续的线性空间,下面测试vector的构造与内存管理:

/*
*vector的构造与内存管理
*/
#include <iostream>
#include <vector>
#include <algorithm>

int main(int argc, char const *argv[])
{
int i;
std::vector<int> iv(2,9);
std::vector<int>::iterator it;
std::cout << "size=" << iv.size() << std::endl;          //size=2
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=2

iv.push_back(1);
std::cout << "size=" << iv.size() << std::endl;          //size=3
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=4
it = iv.begin();
std::cout << "iv.begin()=" << *it << std::endl;          //iv.begin()=9

iv.push_back(2);
std::cout << "size=" << iv.size() << std::endl;          //size=4
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=4

iv.push_back(3);
std::cout << "size=" << iv.size() << std::endl;          //size=5
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=8
std::cout << "iv.begin()=" << *it << std::endl;          //iv.begin()为伪随机数

iv.push_back(4);
std::cout << "size=" << iv.size() << std::endl;          //size=6
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=8

for(i = 0; i<iv.size(); ++i)
std::cout << iv[i] << ' ';
std::cout <<std::endl;                                    //9 9 1 2 3 4

iv.pop_back();
iv.pop_back();
iv.push_back(2);

std::cout << "size=" << iv.size() << std::endl;          //size=5
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=8

it = find(iv.begin(), iv.end(), 2);                      //在[begin(),end())范围内寻找第一个2出现的位置
if (it != iv.end()) iv.insert(it, 3, 7);                 //若未找到2,it指向end()

std::cout << "size=" << iv.size() << std::endl;          //size=8
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=8
for(it = iv.begin(); it != iv.end(); it++)
std::cout << *it <<' ';                              //9 9 1 7 7 7 2 2
std::cout << std::endl;

iv.clear();
std::cout << "size=" << iv.size() << std::endl;          //size=0
std::cout << "capacity=" << iv.capacity() << std::endl;  //capacity=8
std::cout << iv[1] << std::endl;                         //9,空间与值都并未释放

return 0;
}


vector在动态增加大小时,另外申请原来两倍大小的空间,然后将原来内容拷贝过来,然后才开始在原来内容之后构造新元素,并释放原空间。因此指向原vector的迭代器就都失效了。

参考书籍:《STL源码剖析》 –侯捷
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl 源码