C++模板技术和STL实战开发(10)——STL容器与算法(4)——关联容器set和map的应用案例

关联容器内部的元素依据其值自动排序,内部由二叉树实现便于查找

C++模板技术和STL实战开发(10)——STL容器与算法(4)——关联容器set和map的应用案例_第1张图片

可以看到上述示例,如果不动态调整内部的元素的排列的话可能会使左边这样通过剪枝便于查找的结构退化成单链表形式,所以set和map是基于平衡二叉树

1.set案例:扩展set集合:实现交、并、差

代码实现:

#include 
#include 
#include 
using namespace std;

class Student
{
public:
	Student(string number, string name) :m_number(number), m_name(name)
	{}

	bool operator<(const Student& s) const
	{
		int mark = m_number.compare(s.m_number);
		return mark < 0 ? true : false;
	}

	string getNumber()const
	{
		return m_number;
	}

	string getName()const
	{
		return m_name;
	}
private:
	string m_number;
	string m_name;
};

ostream& operator<<(ostream& os, const Student& s)
{
	os << s.getNumber() << "\t" << s.getName() << endl;
	return os;
}

template ,typename A=allocator>
class MySet:public set
{
public:
	MySet(const T* first, const T* last) :set(first, last)
	{}

	//并
	void add(MySet& second)
	{
		for(typename set::iterator iter = second.begin();
			iter != second.end(); iter++)
		{
			set::insert(*iter);
		}
	}

	//交
	void interSect(MySet& second)
	{
		//定义临时空的集合
		set mid;
		//遍历当前集合,如果当前元素在第二个集合中的话,加入临时集合中
		for (typename set::iterator iter = this->begin();
			iter != this->end(); iter++)
		{
			if (second.find(*iter) != second.end())
			{
				mid.insert(*iter);
			}
		}
		//利用swap函数交换临时集合
		set::swap(mid);
	}

	//差
	void difference(MySet& second)
	{
		//定义临时空的集合
		set mid;
		//遍历当前集合,如果当前元素不在第二个集合中的话,加入临时集合中
		for (typename set::iterator iter = this->begin();
			iter != this->end(); iter++)
		{
			if (second.find(*iter) == second.end())
			{
				mid.insert(*iter);
			}
		}
		//利用swap函数交换临时集合
		set::swap(mid);
	}

	void show()
	{
		for (typename set::iterator iter = this->begin();
			iter != this->end(); iter++)
		{
			cout << *iter << endl;
		}
	}
};

int main()
{
	Student s[2] = {
		{Student("1001","Lily")},
		{Student("1002","Lucy")}
	};
	Student t[3] = {
	{Student("2001","Mark")},
	{Student("1001","Lily")},
	{Student("2002","Liber")}
	};

	MySet m1(s,s+2);
	MySet m2(t, t + 3);

	m1.show();
	m2.show();

	cout << "\n-------测试并-------" << endl;
	m1.add(m2);
	m1.show();
	cout << "\n-------测试交-------" << endl;
	MySet m3(s, s + 2);
	MySet m4(t, t + 3);
	m3.interSect(m4);
	m3.show();
	cout << "\n-------测试差-------" << endl;
	MySet m5(s, s + 2);
	MySet m6(t, t + 3);
	m5.difference(m6);
	m5.show();
}

测试:

C++模板技术和STL实战开发(10)——STL容器与算法(4)——关联容器set和map的应用案例_第2张图片

2.Map案例:同义词词典

代码实现:

//CWord:对用户输入一组记录进行同义词封装
//CWordMgr:对每组记录使用map进行管理:插入,显示
#include 
#include 
#include 
#include 
#include 
using namespace std;

//构建同义词基础类
class CWord
{
public:
	CWord(string line)
	{
		istringstream in(line);
		in >> m_mainword;     //假定第一个单词是关键词

		string mid="";
		//剩下的其余单词,都认为是同义词
		while (!in.eof())
		{
			in >> mid;
			m_vecWord.push_back(mid);
		}
	}

	string GetMainWord()
	{
		return m_mainword;
	}

	void show()
	{
		cout << "单词是" << m_mainword << endl;
		cout << "同义词是:" << "\t";
		for (size_t i = 0; i < m_vecWord.size(); i++)
		{
			cout << m_vecWord[i] << "\t";
		}
		cout << endl;
	}
private:
	string m_mainword;//一个单词
	vector m_vecWord;//多个同义词
};

class CWordMgr
{
public:
	//添加同义词
	bool add(string line)
	{
		CWord word(line);
		pair p(word.GetMainWord(),word);
		m_myMap.insert(p);
		return true;
	}

	//显示待查的同义词
	void show(string strFind)
	{
		multimap::iterator iter = m_myMap.find(strFind);
		if (iter != m_myMap.end())
		{
			CWord& temp = iter->second;
			temp.show();
		}
		else
		{
			cout << strFind << "不在字典中" << endl;
		}
	}

	void show()
	{
		for (multimap::iterator iter = m_myMap.begin();
			iter != m_myMap.end(); iter++)
		{
			CWord& temp = iter->second;
			temp.show();
		}
	}
private:
	multimap m_myMap;
};

int main()
{
	string s[] = {
		string("one single unique"),
		string("correct true right"),
		string("near close"),
		string("strong powerful"),
		string("happy pleased")
	};

	CWordMgr mgr;
	for (int i = 0; i < 5; i++)
	{
		mgr.add(s[i]);
	}
	mgr.show();
	cout << "-----------------------" << endl;
	mgr.show("happy");
	cout << "-----------------------" << endl;
	mgr.show("China");
}

测试:

C++模板技术和STL实战开发(10)——STL容器与算法(4)——关联容器set和map的应用案例_第3张图片

你可能感兴趣的:(C++的STL使用和源码学习)