STL之樹形結構關聯式容器

哈希結構鏈接

關聯式容器:樹形結構

文件: 許多異常的IP地址,找到出現次數最多的前K個IP地址

  1. 可能需要統計每個IP地址出現次數
  2. 藉助優先級隊列–堆 找到前K個IP地址

<IP,次數> <英文單詞,中文含義>

關於有序的序列,O(logN)

map: <key,value>

要求:key一定不能重複

#include <iostream>
#include<map>
#include<string>
using namespace std;
//insert
void Testmap1()
{
	map<string, string> m;
	m.insert(pair<string, string>("宋江", "及時雨"));
	m.insert(pair<string, string>("李逵", "黑旋風"));

	//pair<iterator,bool>
	//iterator:代表map中的一個key-value的鍵值對
	//bool:insert插入是否成功
	auto ret=m.insert(make_pair("孫二孃", "母夜叉"));
	if (ret.second)
	{
		cout << ret.first->first << "----" << ret.first->second << endl;
	}
	ret=m.insert(make_pair("李逵", "鐵牛"));
	if (ret.second)
	{
		cout << (*ret.first).first << "----" << ret.first->second << endl;
	}
	cout << m.size() << endl;
	cout << m["李逵"] << endl;

	//用戶提供key--->[]返回與key所對應的value
	m["李逵"] = "鐵牛";
	cout << m["李逵"] << endl;
	m["林沖"] = "豹子頭";
	cout << m["林沖"] << endl;
	cout << m.size() << endl;
}
//遍歷
void Testmap2()
{
	int array[] = { 3,1,9,4,0,7,6,2,5,8 };
	map<int, int> m;
	for (auto e : array)
		m.insert(make_pair(e, e));
	
	//測試:按照迭代方式進行遍歷,能否的到一個關於key有序的序列
	auto it = m.begin();
	while (it!=m.end())
	{
		cout << it->first << "--->" << it->second << endl;
		++it;
	}
	cout << endl;

    //刪除,也可以遍歷find刪除
	m.erase(5);

	for (auto &e:m)
	{
		cout << e.first << "--->" << e.second << endl;
	}
	cout << endl;
}
int main()
{
	Testmap2();
	return 0;
}

set: key

要求:key一定不能重複

#include <iostream>
#include<set>
using namespace std;

//set:key一定是唯一的
//O(logN)
//去重 並排序
int main()
{
	int array[] = { 3,1,9,4,0,7,6,2,5,8 };
	set<int> s;
	for (auto e:array)
	{
		s.insert(e);
	}
	cout << s.size() << endl;
	for (auto e:s)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

multimap: <key,value> key是可以重複的

去重 排序

#include <iostream>
#include<string>
#include<map>
using namespace std;

void Testmap1()
{
	multimap<string, string> m;
	m.insert(pair<string, string>("宋江", "及時雨"));
	m.insert(pair<string, string>("李逵", "黑旋風"));

	//pair<iterator,bool>
	//iterator:代表map中的一個key-value的鍵值對
	//bool:insert插入是否成功
	auto ret = m.insert(make_pair("孫二孃", "母夜叉"));
	ret=m.insert(make_pair("李逵", "鐵牛"));
	cout << m.size() << endl;
}
int main()
{
	Testmap1();
	return 0;
}

key可以重複

在這裏插入圖片描述

multiset: key key可以重複的

只排序

#include <iostream>
#include<set>
using namespace std;

//multiset:只存儲key,key可以重複,關於key有序的序列
int main()
{
	int array[] = { 3,1,9,4,0,7,6,2,5,8,3,1,9,4,0,7,6,2,5,8 };
	multiset<int> s;
	for (auto e : array)
	{
		s.insert(e);
	}
	cout << s.size() << endl;
	for (auto e : s)
	{
		cout << e << " ";
	}
	cout << endl;
	return 0;
}

set
在這裏插入圖片描述
multiset
在這裏插入圖片描述

map的模擬簡單實現(基於紅黑樹)

紅黑樹

<key,val>比較方式–鍵值對中的key

Map.hpp

#pragma once
#include "RBTree.hpp"
namespace bite
{
	//只需要封裝住一個紅黑樹
	template<class K, class V>
	class map
	{
		typedef pair<K, V> ValueType;
		struct KeyOfValue
		{
			const K& operator()(const ValueType& data)
			{
				return data.first;
			}
		};
		typedef RBTree<ValueType, KeyOfValue> RBTree;
	public:
		typedef typename RBTree::Iterator iterator;
	public:
		map()
			:_t()
		{}
		iterator begin()
		{
			return _t.begin();
		}
		iterator end()
		{
			return _t.end();
		}
		pair<iterator, bool> insert(const ValueType& data)
		{
			return _t.Insert(data);
		}
		size_t size()const
		{
			return _t.Size();
		}
		bool empty()const
		{
			return _t.Empty();
		}
		iterator find(const K& key)
		{
			return _t.Find(make_pair(key,V()));
		}

		V& operator[](const K& key)
		{
			return (*((this->insert(make_pair(key, V()))).first)).second;
		}
	private:
		RBTree _t;
	};
}
void TestMap()
{
	bite::map<std::string, std::string> m;
	m.insert(pair<std::string, std::string>("1111", "1111"));
	m.insert(make_pair("1111", "1111"));
	m["0000"] = "0000";
	cout << m.size() << endl;
	for (auto e : m)
		cout << e.first << " " << e.second << endl;
	cout << endl;
}

set的簡單模擬實現(基於紅黑樹)

紅黑樹

key >比較方式–直接用其元素比較

#pragma once
#include "RBTree.hpp"
namespace bite
{
	//只需要封裝住一個紅黑樹
	template<class K>
	class set
	{
		typedef K ValueType;
		struct KeyOfValue
		{
			const K& operator()(const ValueType& data)
			{
				return data;
			}
		};
	public:
		typename typedef RBTree<ValueType,KeyOfValue>::Iterator iterator;
	public:
		set()
			:_t()
		{}
		iterator begin()
		{
			return _t.begin();
		}
		iterator end()
		{
			return _t.end();
		}
		pair<iterator, bool> insert(const ValueType& data)
		{
			return _t.Insert(data);
		}
		size_t size()const
		{
			return _t.Size();
		}
		bool empty()const
		{
			return _t.Empty();
		}
		iterator find(const K& key)
		{
			return _t.Find(key);
		}

	private:
		RBTree<ValueType, KeyOfValue> _t;
	};
}
void TestSet()
{
	bite::set<std::string> s;
	s.insert("1111");
	s.insert("1111");
	cout << s.size() << endl;
	for (auto e : s)
		cout << e << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章