C++STL之vector与list

关于vector的用法

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
using namespace std;

class PtrInt
{
	int* ptr;
public:
	PtrInt(int x = 0) :ptr(new int[x])
	{
		cout << "Create PtrInt:" << endl;
	}
	PtrInt(const PtrInt& pt) :ptr(new int())
	{
		if (pt.ptr!=nullptr)
		{
			*ptr = *pt.ptr;
		}
		cout << "Copy Create:" << endl;
	}
	PtrInt& operator=(const PtrInt& pt)
	{
		if (this != &pt)
		{
			delete ptr;
			ptr = new int();
			if (pt.ptr != nullptr)
			{
				*ptr = *pt.ptr;
			}
			cout << "operator=()" << endl;
		}
	}
	PtrInt(PtrInt&& pt) :ptr(pt.ptr)
	{
		pt.ptr = nullptr;
		cout << "move create" << endl;
	}
	PtrInt& operator=(PtrInt&& pt)
	{
		if (this != &pt)
		{
			delete ptr;
			ptr = pt.ptr;
			pt.ptr = nullptr;
		}
		cout << "move operator=()" << endl;
	}
	~PtrInt()
	{
		delete ptr;
		ptr = nullptr;
		cout << "Destory PtrInt" << endl;
	}
	void Print()const
	{
		if (ptr != nullptr)
		{
			cout << *ptr << endl;
		}
	}
	void SetValue(int x)
	{
		if (ptr = nullptr)
		{
			*ptr = x;
		}
		else
		{
			ptr = new int(x);
		}
	}
	int GetValue()const
	{
		if (ptr != nullptr)
		{
			return *ptr;
		}
		else
		{
			return -1;
		}
	}
};

int main()
{
	std::vector<PtrInt>ar;
	for (int i = 0; i < 20; ++i)
	{
		ar.push_back(PtrInt(i));//构建无名对象
		cout << "Size:" << ar.size() << endl;;
		cout << "capacity" << ar.capacity() << endl;
	}
}

#if 0
class Int
{
private:
	int value;
public:
	Int(int x=0):value(x){}
	~Int(){}
};

int main()
{
	std::vector<int>iar = {12,23,34,45,56,67,78,89};
	std::vector<double>dar;
	std::vector<int*>par;//最好不要存放指针,因为new开辟空间,一定不要忘记析构
	std::vector<int>::reverse_iterator rit = iar.rbegin(); //逆向迭代器

	int n = iar.size();
	int* p = iar.data();
}
#endif

运行结果分析:
在这里插入图片描述

容量按照1.5倍增加,容量增容意味着要不断的创建空间,并且不断调用移动构造函数,和不断将对象析构,效率太低,可以直接调用reserve

int main()
{
	std::vector<PtrInt>ar;
	ar.reserve(200); //先预留200个空间,效率提高,只申请空间,不创建对象
	ar.resize(200); //改变存储元素的个数,直接创建200个对象,并且容量为200
	for (int i = 0; i < 20; ++i)
	{
		ar.push_back(PtrInt(i));//构建无名对象
		cout << "Size:" << ar.size() << endl;;
		cout << "capacity" << ar.capacity() << endl;
	}
}

迭代器失效,指针指向的对象已经被释放掉

int main()
{
	std::vector<PtrInt>ar;
	ar.push_back(PtrInt(1));
	ar.push_back(PtrInt(2)); 
	ar.push_back(PtrInt(3));
	ar.push_back(PtrInt(4));
	ar.push_back(PtrInt(5));

	std::vector<PtrInt>::iterator it = ar.begin();
	ar.insert(it, PtrInt(6));  //调用移动赋值函数

	for (auto x: ar)
	{
		x.Print();
	}
	ar.pop_back();
	ar.erase(it); //迭代器失效,迭代器面向对象的指针
}

vector作为数组,如何使效率变高?(提前获取vector的大小,预留好空间)

关于List的用法

在相同的空间中,vector的存储密度比List的存储密度大

int main()
{
	std::list<PtrInt>arlist;
	for (int i = 0; i < 5; i++)
	{
		//arlist.push_back(PtrInt(i));
		arlist.emplace_back(i);
	}

	for (auto& x : arlist)  //尽量使用引用,因为使用引用不需要调动拷贝构造
	{
		x.Print();
	}
}

出现内存泄漏的情况

int main()
{
	std::list<PtrInt*>arlist;
	for (int i = 0; i < 5; ++i)
	{
		arlist.push_back(new PtrInt(i));
	}
	for (auto& x : arlist)
	{
		x->Print();  //无法调用析构函数去析构PtrInt
	}
	return 0;
}

在这里插入图片描述

解决方案(智能指针)

int main()
{
	std::list<std::unique_ptr<PtrInt>>arlist;
	for (int i = 0; i < 10; ++i)
	{
		arlist.push_back(std::unique_ptr<PtrInt>(new PtrInt(i))); //智能指针,防止内存泄漏
	}
	for (auto& x : arlist)
	{
		x->Print();
	}
	return 0;
}

在这里插入图片描述
List的排序算法(当排序量较少是,一般采用快排)

vector和list的区别

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
关于迭代器失效
在这里插入图片描述
如果需要高效的随机存取,选择vector(深入理解计算机系统1,6,9章)
场景题目一:页面置换算法,如何设计一个页面置换算法
场景题目二:刷视频,如何让视频刷的更快(生产者消费者模型)
在这里插入图片描述
关于vector里面存放指针的情况
在这里插入图片描述

补充点:关于push_back和emplace_back的区别
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淡蓝色的经典

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值