目录
一.引言
众所周知,常用的数据结构包括数组(array),链表((list),树(tree),堆栈(stack),队列(queue),散列表(hash table),集合(set),映射(map)等等。根据在容器中的排列特性,可以将这些数据结构分为序列式(sequence)和关联式(associative)两种。序列容器包括vector,list,deque,stack,queue,heap,priority_queue,slist等等。本章我们从Vector开始讲起。
vector的数据安排以及操作方式,与array非常相似。array是静态空间,vector是动态空间。vector的实现技术,关键在于其对大小的控制以及重新配置时数据移动的效率。一旦vector旧有空间满载,如果客户端每增加一个元素,vector便会动态分配足够的空间。
二.vector包含的方法
2.1初始化方法
vector初始化的各种方法如下:
2.2 赋值运算符重载
赋值运算符重载的方法:
2.3 Vector基本操作
2.3.1 assign用法
assign用法代码如下:
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> first;
std::vector<int> second;
std::vector<int> third;
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.
std::cout << "Size of first: " << int(first.size()) << std::endl;
std::cout << "Valus of first: " << std::endl;
for ( unsigned i = 0; i < first.size(); i++)
{
std::cout << first.at(i) << std::endl;
}
std::cout << "Size of second: " << int(second.size()) << std::endl;
std::cout << "Valus of second: " << std::endl;
for (unsigned i = 0; i < second.size(); i++)
{
std::cout << second.at(i) << std::endl;
}
std::cout << "Size of third: " << int(third.size()) << std::endl;
std::cout << "Valus of third: " << std::endl;
for (unsigned i = 0; i < third.size(); i++)
{
std::cout << third.at(i) << std::endl;
}
return 0;
}
输出结果如下:
2.3.2 Vector常用方法介绍
以下是Vector定义的源代码摘录
//alloc是 SGI STL的空间适配器(后续会做详细总结)
template < class T,class Alloc = alloc>
class Vector
{
public:
Vector();
~ Vector();public:
//Vector 的嵌套型别定义
typedef T value_type;
typedef value_type* pointer;
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;protected:
//下面 simple_alloc是SGI STL 的空间适配器
typedef simple_alloc<value_type, Alloc> data_allocator;
iterator start; //表示目前使用空间的头
iterator finish; //表示目前使用空间的尾
iterator end_of_storage; //表示目前可用空间的尾public:
iterator begin() { return start; } //南抒一梦: 返回第一个元素的迭代器
iterator end() { return finish; } //南抒一梦: 返回最后一个元素的迭代器
size_type size() const { return size_type(end() - begin()); }
size_type capacity() const { return size_type(end_of_storage - begin()); }bool empty() const { return begin() == end(); }
// 南抒一梦:[]运算符重载
reference operator[](size_type n){ return *(begin() + n); }
reference front() { reference *begin(); } //南抒一梦:返回第一个元素的应用 &
reference back() { return *(end() - 1); } //南抒一梦:返回最后一个元素的引用
//将元素插入尾端
void push_back(const T& x)
{
if (finish != end_of_storage)
{
construct(finish, x); //标准库全局函数
++finish;
}
else
{
inset_aux(end(), x);
}
}//将最尾端元素取出
void pop_back()
{
--finish;
destroy(finish); //全局析构函数
}//清除某位置上的元素
iterator erase(iterator position)
{
if (position + 1 != end())
{
copy(position + 1, finish, position); //后续元素往前移动
--finish;
destroy(finish);
return position;
}
}void resize(size_type new_size, const T&x)
{
if (new_size < size())
{
erase(begin() + new_size, end());
}
else
{
inserter(end(), new_size - size(), x);
}
}
void clear() { erase(begin(), end()); }
};
具体使用代码如下:
#include <iostream>
#include <istream>
#include <ostream>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> myVec(4, 10);
unsigned count = myVec.size();
std::cout << "myVec's size is :" << count << std::endl;
std::cout << "myVec's values is :" << count << std::endl;
for (unsigned i = 0; i < myVec.size(); i++)
{
std::cout << myVec[i] << " ";
}
std::cout << std::endl;
std::vector<int>::iterator iterBegin = myVec.begin();
std::vector<int>::iterator iterEnd = myVec.end();
std::vector<int>::reference iterFront = myVec.front();
std::vector<int>::reference iterBack = myVec.back();
std::vector<int>::const_iterator iterCbegin = myVec.cbegin();
std::vector<int>::const_iterator itercEnd = myVec.cend();
unsigned Storage = myVec.capacity();
std::cout << "push_back before capacity: "<< Storage << std::endl;
myVec.push_back(15);
myVec.push_back(20);
Storage = myVec.capacity();
std::cout << "push_back after capacity: " << Storage << std::endl;
for (unsigned i = 0; i < myVec.size(); i++)
{
std::cout << myVec[i] << " ";
}
std::cout << std::endl;
if (!myVec.empty())
{
myVec.erase(myVec.end());
myVec.resize(myVec.size(), 30);
}
for (unsigned i = 0; i < myVec.size(); i++)
{
std::cout << myVec[i] << " ";
}
std::cout << std::endl;
myVec.clear();
system("pause");
//return 0;
}
输出结果:
三. Vector的构造与内存管理
首先我们来做一个小小的测试,看看vector的存储空间大小是如何分配的,测试代码如下:
#include <iostream>
#include <istream>
#include <ostream>
#include <vector>
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> myVec(2, 9);
for (unsigned i = 0; i < 9; i++)
{
myVec.push_back(i);
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
}
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
myVec.push_back(1);
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
myVec.push_back(2);
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
myVec.push_back(3);
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
myVec.push_back(4);
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
myVec.push_back(5);
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
std::cout << "myVec's values is : "<< std::endl;
for (unsigned i = 0; i < myVec.size(); i++)
{
std::cout << myVec.at(i) << " ";
}
std::cout << std::endl;
myVec.pop_back();
myVec.pop_back();
myVec.pop_back();
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
std::cout << "myVec's values is : " << std::endl;
for (unsigned i = 0; i < myVec.size(); i++)
{
std::cout << myVec.at(i) << " ";
}
std::cout << std::endl;
myVec.clear();
std::cout << "myVec's size is : " << myVec.size() << std::endl;
std::cout << "myVec's capacity is : " << myVec.capacity() << std::endl;
system("pause");
//return 0;
}
输出结果如下:
从结果我们看出,Vector的内存大小是按照我们不断在往里面添加数据的时候不断申请内存的。之前的vector大小的动态增加是以原大小的两倍另外配置一块较大空间,然后将原内容拷贝过来,然后才开始原内容之后的构造新元素,并释放原空间。因此,对vector的任何操作,一旦引起空间的重新配置,指向原vector的所有迭代器就失效了。可是我们从上面的例子中可以看出Vector的空间大小并非按照之前大小的两倍进行分配,好像是第一次申请比之前多一个,第二次多两个……,以此类推,不知道是不是Vector库有了新的变动,也欢迎喜欢探究的小伙伴跟我一起查查这是什么原因造成的,欢迎下方留言。
【上一篇:】STL标准模板库用法总结——总纲