STL容器之vector

1、构造初始化

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//构造函数原型:
	//vector v;					 //采用模板实现类实现,默认构造函数
	//vector(v.begin(), v.end());	//将v[begin(), end())区间中的元素拷贝给本身
	//vector(n, elem);				//构造函数将n个elem拷贝给本身
	//vector(const vector &vec);	//拷贝构造函数


	vector<int> v1;											//默认构造
	int arr[] = { 10,20,30,40 };
	vector<int> v2(arr, arr + sizeof(arr) / sizeof(int));	//数组构造
	vector<int> v3(v2.begin(), v2.end());					//迭代器构造
	vector<int> v4(v3);										//拷贝构造
	PrintVector(v2);
	PrintVector(v3);
	PrintVector(v4);
}

int main()
{
	test01();


	return 0;
}

2、赋值操作

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//vector& operator=(const vector &vec);	//重载等号操作符
	//assign(beg, end);						//将[beg, end)区间中的数据拷贝赋值给本身
	//assign(n, value);						//将n个value拷贝赋值给本身

	int arr[] = { 10, 20, 30, 40 };
	vector<int> v1(arr, arr + sizeof(arr) / sizeof(int));	//默认构造

	//assign():
	vector<int> v2;
	v2.assign(v1.begin(), v1.end());

	//assign():
	vector<int> v3;
	v3.assign(5, 1000);	//也即是5个元素,都是1000

	//重载 = 运算符
	vector<int> v4;
	v4 = v3;

	//交换
	int array[] = { 100, 200, 300, 400 };
	vector<int> v5(array, array + sizeof(array) / sizeof(int));
	v5.swap(v1);


	PrintVector(v1);
	PrintVector(v2);
	PrintVector(v3);
	PrintVector(v4);
	PrintVector(v5);
}

int main()
{
	test01();


	return 0;
}

3、容量和大小

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//empty();						//判断容器是否为空
	//capacity();					 //容器的容量
	//size();						//返回容器中元素的个数
	//resize(int num);				//重新指定容器的长度为num,若容器变长,则以默认值0填充新位置;
															//若果容器变短,则末尾超出容器长度的元素被删除。

	//resize(int num, elem);		//重新指定容器的长度为num,若容器变长,则以指定值elem填充新位置;
															//如果容器变短,则末尾超出容器长度的元素被删除。
	vector<int> v;
	for (int i = 1; i <= 4; i++)
		v.push_back(i * 10);

	cout << "capacity: " << v.capacity() << endl;
	cout << "size: " << v.size() << endl;
	cout << "empty? " << v.empty() << endl;
	PrintVector(v);
	cout << "----------------------------------------" << endl;
	v.resize(6);		//变长,新位置填充默认0
	PrintVector(v);
	//cout << v.size() << endl;			//6
	//cout << v.capacity() << endl;		//6
	v.resize(2);		//变短,删除末尾4个元素
	PrintVector(v);
	//cout << v.size() << endl;			//2
	//cout << v.capacity() << endl;		//6

	cout << "----------------------------------------" << endl;
	v.resize(4, 88);	//变长,新位置填充为指定值88
	PrintVector(v);
	v.resize(2);		//变短,删除末尾2个元素
	PrintVector(v);


}


int main()
{
	test01();
	return 0;
}

4、存取操作

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//at(int idx);	 //返回索引idx所指的数据
	//operator[];	 //返回索引idx所指的数据
	//front();		//返回容器中第一个数据元素
	//back();		//返回容器中最后一个数据元素


	int array[] = { 100, 200, 300, 400 };
	vector<int> v(array, array + sizeof(array) / sizeof(int));
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
	cout << "------------" << endl;
	for (int i = 0; i < v.size(); i++)
		cout << v.at(i) << " ";
	cout << endl;

	cout << v.front() << endl;
	cout << v.back() << endl;
}

int main()
{
	test01();


	return 0;
}

5、插入删除操作

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	//push_back(elem);									//尾部插入元素
	//insert(const_iterator pos, elem);					//迭代器指向位置pos插入元素elem
	//insert(const_iterator pos, int count, elem);		//迭代器指向位置pos插入count个元素elem
	//insert(pos, beg, end);							//在pos位置的前面插入[beg,end)区间的数据,无返回值

	//pop_back();										//删除最后一个元素
	//erase(const_iterator pos);						//删除迭代器指向的元素
	//erase(const_iterator start, const_iterator end);	//删除迭代器从start到end之间的元素
	//clear();											//删除容器中所有元素
	int array[] = { 10, 20, 30, 40 };
	vector<int> v(array, array + sizeof(array) / sizeof(int));

	//insert():可以这么理解,在迭代器指向的位置的前面插入元素
	v.insert(v.begin(), 336);		
	PrintVector(v);	//336,10,20,30,40
	v.insert(v.begin() + 2, 100);	//vector支持随机访问,否则不能直接+2,,,,等操作
	PrintVector(v);	//336,10,100,20,30,40
	//支持数组下标的,一般都支持随机访问;此时迭代器可以直接+2,-3,+5,,,等操作
	v.insert(v.begin() + 1, 3, 88);
	PrintVector(v);
	v.insert(v.begin(), array, array + sizeof(array) / sizeof(int));
	PrintVector(v);


	cout << "----------------------------------" << endl;
	//删除操作:erase()
	v.erase(v.begin());
	PrintVector(v);
	v.erase(v.begin()+2, v.begin() + 6);	//实际上就是[begin,end)这个区间元素
	PrintVector(v);							
	v.clear();
	PrintVector(v);
	cout << "size: " << v.size() << endl;
}

int main()
{
	test01();
	return 0;
}

6、利用swap缩减空间

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void PrintVector(vector<int>&& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> v;
	for (int i = 0; i < 100000; i++)
		v.push_back(i);
	cout << "size: " << v.size() << endl;
	cout << "capacity: " << v.capacity() << endl;

	cout << "--------------------------" << endl;
	v.resize(10);
	cout << "size: " << v.size() << endl;
	cout << "capacity: " << v.capacity() << endl;
	PrintVector(v);

	cout << "------------缩减空间容量------------" << endl;
	//PrintVector(vector(v));
	//cout << vector(v).size() << endl;
	vector<int>(v).swap(v);
	cout << "size: " << v.size() << endl;
	cout << "capacity: " << v.capacity() << endl;
}

int main()
{
	test01();


	return 0;
}

7、reverse预留空间

#include 
#include 
using namespace std;

void PrintVector(vector<int>& v)
{
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	vector<int> v;
	int count = 0;			//统计空间扩展次数
	int* p = NULL;
	v.reserve(100000);		//预留空间
	for (int i = 0; i < 100000; i++)
	{
		v.push_back(i);
		if (p != &v[0])
		{
			p = &v[0];
			count++;
		}
	}
	cout << "count: " << count << endl;
	//vector动态扩展原理:
	//并不是在原空间之后续接新空间,而是找更大的内存空间,然后将原数据拷贝新空间,释放原空间!

	//预留空间reverse的作用:减少vector在动态扩展容量时的扩展次数!
	//如果数据较大,可以一开始就利用revese预留空间!
}


int main()
{
	test01();
	return 0;
}

8、自动扩增

//自动扩增
//当容量不够时,将以当前容量的2倍进行容量扩增!(错误,通过实验发现,并不是以2的倍数进行扩增的)
void test01()
{
	vector<int> v;
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(1);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(2);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(3);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(4);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(5);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(6);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(7);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(8);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(9);
	cout << "capacity:" << v.capacity() << endl;
	v.push_back(10);
	cout << "capacity:" << v.capacity() << endl;
	//插入1, 2, 3, 4的时候,容量每次扩增一个,
	//当插入5的时候,容量扩增了2个,
	//当插入7的时候,容量扩增了3个!
	//当插入10的时候,容量扩增了4个!
}

STL容器之vector_第1张图片

9、遍历


//反向迭代器reverse_iderator
void test02()
{
	//正向迭代器遍历
	vector<int> v; 
	for (int i = 0; i < 5; i++)
		v.push_back(i);
	cout << "-----------------iterator-------------------------" << endl;

	for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
		cout << *it << " ";
	//使用反向迭代器遍历
	cout << endl << "-----------------reverse_iterator-------------------------" << endl;
	for (vector<int>::reverse_iterator rit = v.rbegin(); rit != v.rend(); rit++)
		cout << *rit << " ";
	cout << endl;
}

/*普通迭代器 和 常迭代器*/
void test03()
{
	cout << "普通对象 对应于 普通迭代器!" << endl;
	cout << "常对象   对应于 常迭代器!" << endl;
	cout << "--------------------普通对象下的迭代器--------------------" << endl;
	vector<int> v1(5,3); 				//普通对象
	for (vector<int>::iterator it = v1.begin(); it != v1.end(); it++)
	{
		cout << *it << " ";
	}

	cout << endl << "---------------------常对象下的迭代器--------------------" << endl;
	const vector<int> v2(5, 2);		//常对象
	for (vector<int>::const_iterator c_it = v2.begin(); c_it != v2.end(); c_it++)
	{
		cout << *c_it << " ";
	}
	cout << endl;
}

你可能感兴趣的:(C++,c++,算法,开发语言)