STL--Vector

一、vector的用法
1.vector概述
vector的数据安和操作方式类似于array,vector类的下标位置也是从0开始的,但array是静态空间,而vector是动态的。
vector维护的是一个线性空间,所以不论其元素类型为何,指针都可以作为vector的迭代器而满足所有必要条件。
2.vector的数据结构
vector采用的数据结构非常简单:线性连续空间,俩个迭代器start(指向使用空间的头)和finish(指向使用空间的尾),并以迭代器end_of_storage指向连续空间(含备用空间)的尾端

template<calss T>
class Vector
{
protected:
iterator start;
iterator finish;
iterator endofstorage;
};

3.vector的空间策略
vector实际上分 的空间大小可能会比用户需要的空间大一些,以备将来扩充。这就是容量的概念(capacity),一个vector的容量永远小于或等于其大小,一旦容量等于大小便是满载,下次再插入时则需要增容(重新 、元素移动、释放原空间)

4.vector的初始化
int main()
{
	vector<int> arr;
	//声明一个int型的变量

	vector<int> arr1(5);
	//声明一个大小为5的int对象

	vector<int> arr2(10, 2);
	//声明一个大小为10,值都为2 的对象

	vector<int> arr3(arr);
	//用已存在的vector对象(arr)初始化arr3对象

	vector<int> arr4(arr.begin(), arr.begin() + 3);
	//用对象arr的arr[0]~arr[2]初始化arr4

	int array[5] = { 1, 2, 3, 4, 5 };
	vector<int> arr5(array, array + 5);
	//将array数组用于初始化arr5

	vector<int> arr6(&array[1], &array[4]);
	//将array[1]~array[4]范围内的元素作为vec的初始值

	system("pause");
	return 0;
}
5.vector容器大小的判断
(1) int size () const //返回容器中元素个数
(2) int capacity() const //返回当前容器所能容纳的最大元素
(3) int max_size() const //返回最大可允许的vector元素数量值

int main()
{
        int array[5] = { 1, 2, 3, 4, 5 };
	vector<int> arr5(array, array + 5);

	cout << "该容器中的元素个数为:" << arr5.size() << endl;
	cout << "该容器的容量为:" << arr5.capacity() << endl;
	cout << "该容器最大可允许vector的元素数量为:" << arr5.max_size() << endl;
	system("pause");
	return 0;
}

6.vector容器内添加元素
(1)尾部增加:void push_back(const T& x)
(2)迭代器指向元素前加入一个元素x:
iterator  insert(iterator it ,const T& x)
(3)迭代器指向元素前增加n个相同的元素x:
iterator insert(iterator it,int n,const T& x)
(4)迭代器指向元素前插入另一个相同类型向量的
代码:
(1)
int main()
{
	vector<int> arr;
	arr.push_back(1);
	arr.push_back(2);
	arr.push_back(3);
	arr.push_back(4);

	vector<int>::iterator it; //迭代器

	it = arr.begin();
	cout << "插入后的vector当中元素分别为:" << endl;
	for (it = arr.begin(); it < arr.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}
(2)
int main()
{
	vector<int> arr(5,1);
	vector<int>::iterator it;
	it = arr.begin();
	it = arr.insert(it, 200);//在迭代器的位置前插入x

	cout << "插入后的vector当中元素分别为:" << endl;
		for (it = arr.begin(); it < arr.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;

	system("pause");
	return 0;
}

(3)
int main()
{
	vector<int> arr(5,1);
	vector<int>::iterator it;
	it = arr.begin();
	arr.insert(it, 2, 300);//在迭代器前加入n个x    (迭代器位置,n ,x)

	cout << "插入后的vector当中元素分别为:" << endl;
		for (it = arr.begin(); it < arr.end(); it++)
		{
			cout << *it << " ";
		}
		cout << endl;

	system("pause");
	return 0;
}
(4)
int main()
{
	vector<int> arr1(5, 1);
	vector<int> arr2(2, 100);
	vector<int>::iterator it;
	it = arr1.begin();

	arr1.insert(it + 2, arr2.begin(), arr2.end());//(插入位置,插入数组的开始,插入数组的结束)

	cout << "插入后的vector当中元素分别为:" << endl;
			for (it = arr1.begin(); it < arr1.end(); it++)
			{
				cout << *it << " ";
			}
	cout << endl;

	int array[]= { 1, 2, 3 };
	arr1.insert(arr1.begin(), array, array + 3);
	cout << "插入后的vector当中元素分别为:" << endl;
	for (it = arr1.begin(); it < arr1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}
7.删除容器的元素
(1)删除迭代器指向的元素:iterator erase(iterator it)
(2)删除最后一个元素void pop_back();
(3)清空所有元素void clear()
代码:
(1)
int main()
{
	vector<int> arr;
	arr.push_back(1); 
	arr.push_back(2);
	arr.push_back(3);
	arr.push_back(4);
	vector<int>::iterator it;
	it = arr.begin();
	arr.erase(it);
	cout << "删除后的vector当中元素为:" << endl;
			for (it = arr.begin(); it < arr.end(); it++)
			{
				cout << *it << " ";
			}
			cout << endl;

	system("pause");
	return 0;
}
(2)
int main()
{
	vector<int> arr(5, 10);
	arr.pop_back();
	vector<int>::iterator it;
	it = arr.begin();
	cout << "删除后的vector当中元素为:" << endl;
			for (it = arr.begin(); it < arr.end(); it++)
			{
				cout << *it << " ";
			}
			cout << endl;
	system("pause");
	return 0;
}
(3)
int main()
{
	vector<int> arr(5, 10);
	//arr.pop_back();
	arr.clear();
	vector<int>::iterator it;
	it = arr.begin();
	cout << "删除后的vector当中元素为:" << endl;
			for (it = arr.begin(); it < arr.end(); it++)
			{
				cout << *it << " ";
			}
			cout << endl;

	system("pause");
	return 0;
}

二、.Myvector的实现
#include<iostream>
#include<assert.h>
using namespace std;

template<class T>
class MyVector
{
public:
	typedef T* Iterator;
	typedef const T* ConsIterator;
public:
	MyVector()
		:_start(NULL)
		, _finish(NULL)
		, _endofstorage(NULL)
	{}
	
	void pushBack(const T& x)
	{
		Iterator end = End();
		Insert(end, x);
	}
	void Insert(Iterator& pos, const T& x)
	{
		size_t n = pos - _start;
		if (_finish == _endofstorage)//满了即增容
		{
			size_t len = Capacity() == 0 ? 3 : Capacity() * 2;
			Expand(len);
		}
		pos = _start + n;
		for (Iterator end = End(); end != pos; --end)//拷数据,将pos以后的数据向后移动
		{
			*end = *(end - 1);
		}
		*pos = x;
		++_finish;
	}

	void Erase(Iterator &pos)
	{
		assert(pos);
		for (Iterator It = pos; It < _finish; ++It)
		{
			_start[It] = _start[It + 1];
			//*It = *(It + 1);
		}
		--_finish
	}
	void popBack()
	{
		if (_start == NULL)
		{
			return;
		}
		--_finish;
	}
	void Resize(size_t n,const T& val =T())
	{
		if (n < Size())
		{
			_finish = _start + n;
		}
		else
		{
			Reserve(n);
			size_t len = n - Size();
			for (size_t i = 0; i < len; i++)
			{
				pushBack();
			}
		}
	}
	void Reserve(size_t n)
	{
		Expand(n);
	}
	size_t Size()
	{
		return _finish - _start;
	}
	size_t Capacity()//容量
	{
		return _endofstorage-_start;//末尾减初始
	}
	Iterator End()
	{
		return _finish;
	}
	Iterator Begin()
	{
		return _start;
	}
	void Expand(size_t n)
	{
		const size_t size = Size();
		const size_t capacity = Capacity();
		if (n > capacity)
		{
			T* tmp = new T[n];//开空间
			for (size_t i = 0; i < size; ++i)//拷数据
			{
				tmp[i] = _start[i];
			}
			delete _start;//释放原来的空间
			_start = tmp;
			_finish = _start+size;
			_endofstorage = _start+n;
		}
	}
	T& operator[](size_t pos)
	{
		assert(pos < Size());
		return _start[pos];
	}

private:
	Iterator _start;
	Iterator _finish;
	Iterator _endofstorage;
};
int main()
{
	MyVector<int> v;
	v.pushBack(1);
	v.pushBack(2);
	v.pushBack(3);
	v.pushBack(4);
	
	MyVector<int>::Iterator it;
	it = v.Begin();
	cout << "vector当中元素分别为:" << endl;
	for (it = v.Begin(); it < v.End(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	v.popBack();
	cout << "删除后vector当中元素分别为:" << endl;
	for (it = v.Begin(); it < v.End(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	it = v.Begin();
	v.Insert(it,10);
	cout << "插入后vector当中元素分别为:" << endl;
	for (it = v.Begin(); it < v.End(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	system("pause");
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值