二叉树映射map
#include<iostream>
#include<map> //映射(也叫字典),二叉树映射,不是哈希映射
using namespace std;
int main()
{
cout << "二叉树(红黑树)" << endl;
map<string, int> m;
m["bill"]= 67;
m["toney"] = 98;
m["Mary"] = 100;
cout << m["Mary"] << endl;
system("pause");
return 0;
}
数组优点
#include<iostream>
#include<string>
using namespace std;
int main()
{
//数组的优点
//对下标(索引)进行非常快的地址运算
//取arr[0] arr[23232] 速度一样
int arr[100000];
int length = sizeof(arr) / sizeof(int);
cout << length << endl;
for (int i = 0; i < length; i++)
{
arr[i] = i%100;
}
cout << arr[8] << endl;
cout << arr[18] << endl;
cout << arr[98756] << endl;
system("pause");
return 0;
}
线性映射
LinearMap.h
#ifndef LINEARMAP_H
#define LINEARMAP_H
#include<vector>
template<class Key,class Value>
class LinearMap //线性映射-没用的东西
{
public:
LinearMap(int size = 101) :arr(size)
{
currentSize = 0;
}
void Put(const Key &k, const Value &v)
{
arr[currentSize] = DataEntry(k, v);
++currentSize;
}
Value Get(const Key & k)
{
//线性查找
for (size_t i = 0; i < currentSize; ++i)
{
if (arr[i].key == k)
{
return arr[i].value;
}
}
return Value();
}
private:
struct DataEntry {
Key key;
Value value;
DataEntry(const Key & k = Key(),
const Value & v = Value()) : key(k), value(v) {}
};
std::vector<DataEntry> arr;
int currentSize;
};
#endif
linearMap.cpp
#include<iostream>
#include<string>
#include"LinearMap.h"
using namespace std;
int main()
{
//没用的,仅学习的线性映射
LinearMap<string, int> lm;
lm.Put("bill", 99);
lm.Put("Tom", 88);
cout << "Lin earMap:" << lm.Get("Tom") << endl;
system("pause");
return 0;
}
运行结果:
哈希映射
HashMap.h
#ifndef LINEARMAP_H
#define LINEARMAP_H
#include<vector>
template<class Key, class Value>
class HashMap //哈希映射 最快
{
public:
HashMap(int size = 101) :arr(size)
{
currentSize = 0;
}
void Put(const Key &k, const Value &v)
{
int pos = myHash(k);
arr[pos] = DataEntry(k, v);
++currentSize;
}
Value Get(const Key & k)
{
int pos = myHash(k);
if (arr[pos].key == k)
return arr[pos].value;
else
return Value();
}
unsigned hash(const Key & k) const
{
unsigned int hashVal = 0;
const char* keyp = reinterpret_cast<const char*>(&k); //转型
for (size_t i = 0; i < sizeof(Key); i++)
{
hashVal = 37 * hashVal + keyp[i];
}
return hashVal;
}
int myHash(const Key & k) const
{
unsigned hashVal = hash(k);
hashVal %= arr.size();
return hashVal;
}
private:
struct DataEntry {
Key key;
Value value;
DataEntry(const Key & k = Key(),
const Value & v = Value()) : key(k), value(v) {}
};
std::vector<DataEntry> arr;
int currentSize;
};
#endif
HashMap.cpp
#include<iostream>
#include<string>
#include"HashMap.h"
using namespace std;
int main()
{
HashMap<string, int> hm;
/*cout << hm.hash("Bill") << endl;
cout << hm.myHash("Bill") << endl;*/
hm.Put("Bill", 33);
hm.Put("Tom", 98);
cout << hm.Get("Bill") << endl;
//cout << hm.Get("Tom") << endl; 取两个就会错误
system("pause");
return 0;
}
运行结果:
c++自带的哈希映射(STL)
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
#include<iostream>
#include<string>
#include<hash_map>
using namespace std;
//使用C++封装好的hash_map
//又要排序又要映射时就用tree_map(二叉树映射)
int main()
{
hash_map<string, int> hm;
hm["Bill"] = 12;
hm["Tom"] = 22;
hm["Mary"] = 34;
cout << hm["Mary"] << endl;
cout << hm["Tom"] << endl;
system("pause");
return 0;
}
运行结果: