堆可以应用于堆排序。
当用堆做排序时一定要重写大于小于运算符重载函数。
哈希统计与大堆小堆。
这东西就是一点一点的积累。
代码量太少了。
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个适配器。
应用只能是通过刷题来实现了。
链式哈希表解决哈希冲突:用链表将冲突的元素保存下来。