c++第22次课__2018.06.03

本文介绍如何使用堆进行排序,并实现数据统计功能。利用C++ STL中的优先队列实现堆排序,通过哈希表和位图进行数据查重。同时探讨了不同数据结构的应用场景,如快排、归并排序等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

堆可以应用于堆排序。

当用堆做排序时一定要重写大于小于运算符重载函数。

哈希统计与大堆小堆。

这东西就是一点一点的积累。

代码量太少了。

topk问题

查重哈希表、bitmap 

找到数据的上限,最大值。

#include <iostream>
#include <unordered_map>
#include <string>
#include <time.h>
#include <queue>
#include <functional>
#include <bitset>//c++STL库中提供了位图
//提供了很多方法、提供了【】运算符重载函数。
using namespace std;

class Pair
{
public:
	Pair(int K, int V) :k(K), v(V) {}

	bool operator<(const Pair &src)const
	{
		if (v < src.v)
		{
			return true;
		}
		return false;
	}
	bool operator>(const Pair &src)const
	{
		if (v > src.v)
		{
			return true;
		}
		return false;
	}
	int k;
	int v;
};

int main()
{
	vector<int> vec;
	vec.reserve(100000);
	srand(time(0));
	for (int i = 0; i < 100000; ++i)
	{
		vec.push_back((rand() % 1000000) + 1);
	}

	unordered_map<int, int> map;
	vector<int>::iterator it = vec.begin();
	unordered_map<int, int>::iterator mapit;
	for (; it != vec.end(); ++it)
	{
		mapit = map.find(*it);
		if (mapit != map.end())
		{
			mapit->second += 1;
		}
		else
		{
			map[*it] = 1;
		}
	}

	//priority_queue<int, vector<int>, greater<int>> MyIntPriority_Queue;
	priority_queue<Pair, vector<Pair>, greater<Pair>> que;
	mapit = map.begin();
	it = vec.begin();
	for (int i = 0; i < 10; ++i)
	{
		que.push(Pair((*mapit).first, (*mapit).second));
		++it;
	}
	for (; mapit != map.end(); ++mapit)
	{
		if (que.top().v<(*mapit).second)
		{
			que.pop();
			que.push(Pair((*mapit).first, (*mapit).second));
		}
	}

	while (!que.empty())
	{
		cout << "key:" << que.top().k<<"	" << "value: " << que.top().v << endl;;
		que.pop();
	}
	return 0;
}

编译之后就内联了

字符串查重:哈希表、位图(int)。

字符串查重(单词)

字典树

外排序:主要就是归并排序。

快排对于完全无序的数字效率最高。

堆的调整就是树的高度。

海量数据查重。

位图查重代码:

#include <iostream>
#include <unordered_map>
#include <string>
#include <time.h>
#include <queue>
#include <functional>
#include <bitset>//c++STL库中提供了位图
//提供了很多方法、提供了【】运算符重载函数。
using namespace std;

class Pair
{
public:
	Pair(int K, int V) :k(K), v(V) {}

	bool operator<(const Pair &src)const
	{
		if (v < src.v)
		{
			return true;
		}
		return false;
	}
	bool operator>(const Pair &src)const
	{
		if (v > src.v)
		{
			return true;
		}
		return false;
	}
	int k;
	int v;
};

class BitMap
{
public:
	BitMap(int size)
	{
		p = new char[size / 8 + 1];
	}
	~BitMap()
	{
		delete[]p;
	}
	int getBitVal(int val)const
	{
		int index = val / 8;
		int bit = val % 8;
		return p[index] & (1 << bit);
	}
	void setBitVal(int val)
	{
		int index = val / 8;
		int bit = val % 8;
		p[index] |= (1<<bit);
	}
private:
	char *p;
};
int main()
{
	vector<int> vec;
	vec.reserve(100000);
	srand(time(0));
	for (int i = 0; i < 100000; ++i)
	{
		vec.push_back((rand() % 1000000) + 1);
	}

	bitset<100000>mybitset;
	for (int i = 0; i < vec.size(); ++i)
	{
		int index = vec[i] / 8;
		int bitIndex = index * 8 + vec[i] % 8;
		if (mybitset[bitIndex] > 0)
		{
			cout << "第一个重复的元素是:" << vec[i] << endl;
			break;
		}
		else
		{
			mybitset[bitIndex] = 1;
		}
	}
	return 0;
}

底层红黑树,中序遍历

set去重。set只存储一个K。map存键值对,用pair对象打包到一起。

STL包含的东西:11个容器 3个适配器。

应用只能是通过刷题来实现了。

链式哈希表解决哈希冲突:用链表将冲突的元素保存下来。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值