0
点赞
收藏
分享

微信扫一扫

【C++教程从0到1入门编程】第九篇:STL中Vector类

青青子衿谈育儿 03-13 17:00 阅读 2

一、vector的介绍

1.vector的介绍

二、vector的使用

1.定义

2.iterator使用

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
using namespace std;
void testvector2()   //遍历数据也是三种方法
{
	vector<int> v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	for (size_t i = 0; i < v1.size(); i++)
	{
		v1[i] = v1[i] * 2;
		cout << v1[i] << " ";
	}
	cout << endl;
	vector<int>::iterator it = v1.begin();  //普通正向迭代器可以读可以写
	while (it != v1.end())
	{
		*it = *it * 2;
		cout << *it << " ";
		it++;
	}
	cout << endl;
	vector<int>::reverse_iterator vit = v1.rbegin();  //reverse逆置  reserve保留
	while (vit != v1.rend())
	{
		cout << *vit << " ";
		vit++;
	}
	cout << endl;
	for (auto e : v1)     //编译器替换成迭代器生成的
	{
		cout << e << " ";
	}
	cout << endl;
}
void print_vector(const vector<int>& v)  //传引用,加引用就加const,const 对象就要用const的迭代器
{
	vector<int>::const_iterator it = v.begin();
	while (it != v.end())
	{
		//*it = *it + 1;  const的迭代器不可以写,修改
		cout << *it << " ";
		it++;
	}
	cout << endl;
}

3.vector 空间增长问题

void testvector3()        //空间增长问题
{
	vector<int>v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.pop_back();
	cout << v1.size() << endl;
	cout << v1.capacity() << endl;
	cout << v1.empty() << endl;

	size_t sz;
    vector<int> foo; 
	//foo.reserve(150);  //提前预留空间,改变容量
	foo.resize(1067);     //改变_size,开辟好了1067个空间,并初始化,后面如果push_back的话在1068下标开始增加空间 
	sz = foo.capacity();
	cout << "making foo grow:" << endl;
	for (int i = 0; i < 100; ++i)
	{
		//foo.push_back(i);
		foo[i] = i;
		if (sz != foo.capacity())
		{
			sz = foo.capacity();
			std::cout << "capacity changed: " << sz << endl;
		}
	}
}

4.vector增删查改

void testvector4()  //vector的增删查改
{
	vector<int> v1;
	v1.push_back(50);
	v1.push_back(-4);
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(100);
	v1.push_back(200);
	v1.pop_back();
	
	for (size_t i = 0; i < v1.size(); i++) //[]访问,失败通过断言,越界实现
	{
		cout << v1[i] << " ";
	}
	cout << endl;
	for (size_t i = 0; i < v1.size(); i++)
	{
		cout << v1.at(i) << " ";          //at访问,失败抛异常
	}
	cout << endl;

	v1.insert(v1.begin(), 9);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	v1.erase(v1.begin());  //删除下标为0的值
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	v1.erase(v1.begin()+1); //删除下标为1的值
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<int>::iterator pos = find(v1.begin(), v1.end(), 100);  //查找算法
	if (pos != v1.end())
	{
		v1.erase(pos);
	}
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	sort(v1.begin(), v1.end());  //排序算法
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	v1.erase(v1.begin(), v1.end());   //全部删除
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;
}

三、vector 迭代器失效问题

       迭代器的主要作用就是让算法能够不用关心底层数据结构,其底层实际就是一个指针,或者是对指针进行了封装,比如:vector的迭代器就是原生态指针T*。因此迭代器失效,实际就是迭代器  底层对应指针所指向的空间被销毁了,而使用一块已经被释放的空间,造成的后果是程序崩溃(即如果继续使用已经失效的迭代器,程序可能会崩溃。

可能引起失效的原因是:

1.会引起其底层空间容量改变的操作,都有可能是迭代器失效,比如:resize、reserve、insert、assign、push_back等
#define _CRT_SECURE_NO_WARNINGS   1
#include<iostream>
#include<vector>
using namespace std;
void testvector1()
{
	vector<int>v1;
	v1.push_back(1);
	v1.push_back(2);
	v1.push_back(3);
	v1.push_back(4);
	v1.push_back(5);
	vector<int>::iterator it = v1.begin();
	while (it != v1.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl;
	v1.push_back(6);
	v1.push_back(7);
	v1.push_back(8);
	while (it != v1.end())
	{
		cout << *it << " ";
		it++;
	}
}

2.指定位置元素的删除操作–erase。

        erase删除pos位置元素后,pos位置之后的元素会往前搬移,没有导致底层空间的改变,理论上讲迭代不应该会失效,但是:如果pos刚好是最后一个元素,删完之后pos刚好是end的位置,而end位置是没有元素的,那么pos就失效了。因此删除vector中任意位置上元素时,vs就认为该位置迭代器失效了。

迭代器失效解决办法:在使用前,对迭代器重新赋值即可。

举报

相关推荐

0 条评论