stl之3 set 与map

本文通过具体的C++代码示例介绍了如何使用模板来实现通用的打印功能,并展示了自定义比较器以改变集合排序方式的方法。此外,还探讨了使用自定义结构作为map键值的比较方式以及多值映射的高级应用。
#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <iterator>
#include <algorithm>

namespace myPrint
{
	template<class T>
	void PRINT_ELEMENT(const T &coll, char *pStr = "")
	{
		typename T::const_iterator pos;
		std::cout << pStr;
		for (pos = coll.begin(); pos!= coll.end(); ++pos)
		{
			std::cout <<*pos << " ";
		}
		std::cout << std::endl;
	}
}

namespace mySet
{   
	template<class T>
	class CompareMode
	{
	public:
		enum com_mod{ normal, reversed};
	public:
		CompareMode(com_mod mod = normal)
			: Mod(mod) { }

		bool operator()(const T &vLeft, const T &vRight) const
		{
			return Mod == normal ? vLeft < vRight : vRight < vLeft;
		}

		bool operator==(const CompareMode &rhs)
		{
			return Mod == rhs.Mod;
		}

	private:
		com_mod Mod;
	};

	typedef std::set<int, CompareMode<int> > IntSet; //有两个构造函数
	
	void fun()
	{ 
		CompareMode<int> sort_mod(CompareMode<int>::reversed);
		IntSet s(sort_mod);

		for (int i = 1; i < 50; ++i)
		{
			int x = rand() % 100;
			s.insert(x);
		}

		myPrint::PRINT_ELEMENT(s);
	}
}

namespace myMap
{
	struct myString
	{
		std::string cmp1;
		std::string cmp2;
		std::string cmp3;
		myString():cmp1(""), cmp2(""), cmp3("") { }
		myString(const std::string &s1, const std::string &s2, const std::string &s3)
			:cmp1(s1), cmp2(s2), cmp3(s3) { }
	};

	class cmpMyString
	{
	public:
		enum com_mod{First, Second, Last};
	public:
		cmpMyString(const com_mod mod = First): Mod(mod) { }

		bool operator() (const myString &vL, const myString &vR)
		{
			if (Mod == First)
			{
				if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;
				else if (vL.cmp2 != vR.cmp2) return vL.cmp2 < vR.cmp2;
				return vL.cmp3 < vR.cmp3;
			}

			if(Mod == Second)
			{
				if (vL.cmp2 != vR.cmp2) return vL.cmp2 < vR.cmp2;
				else if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;
				return vL.cmp3 < vR.cmp3;
			}

			if(Mod == Last)
			{
				if (vL.cmp3 != vR.cmp3) return vL.cmp3 < vR.cmp3;
				else if (vL.cmp1 != vR.cmp1) return vL.cmp1 < vR.cmp1;
				return vL.cmp2 < vR.cmp2;
			}
		}

	private:
		com_mod Mod;
	};

	typedef std::map<myString, int, cmpMyString> StringIntMap;

	void print(const std::pair<myString, int> &p)
	{
		std::cout << p.first.cmp1 << " "
			      << p.first.cmp2 << " "
				  << p.first.cmp3 << " " 
				  << p.second << std::endl;
	}

	void fun()
	{
		//StringIntMap mp((cmpMyString(cmpMyString::Second)));
		StringIntMap mp(cmpMyString(cmpMyString::Second));
		//waring warning C4930: “myMap::StringIntMap 
		//mp(myMap::cmpMyString)”: 未调用原型函数(是否是有意用变量定义
		mp.insert(std::pair<myString, int>(myString("1", "3", "4"), 1));//error C2228: “.insert”的左边必须有类/结构/联合
		mp.insert(std::pair<myString, int>(myString("1", "2", "4"), 2));
		mp.insert(std::pair<myString, int>(myString("2", "3", "4"), 3));
		mp.insert(std::pair<myString, int>(myString("3", "2", "4"), 4));
		mp.insert(std::pair<myString, int>(myString("1", "2", "1"), 5));
		mp.insert(std::pair<myString, int>(myString("1", "3", "5"), 6));

		for_each(mp.begin(), mp.end(), print); std::cout << std::endl;

		myString s1("1", "3", "4");
		myString s2("2", "5", "4");
		std::swap(s1, s2);

		std::cout << s1.cmp1 << " "
			      << s1.cmp2 << " "
			      << s1.cmp3 << " " 
				  << std::endl;

		std::cout << s2.cmp1 << " "
			      << s2.cmp2 << " "
			      << s2.cmp3 << " " 
			      << std::endl;
	}
        
        void print1(const std::pair<int, int> &p)
	{
		std::cout << p.first << " "
			      << p.second << std::endl;
	}
        typedef std::multimap<int, int> IntIntMap;
	void fun1()
	{
		IntIntMap mp;
		
		for(int i = 0; i < 25; ++i)
		{
			mp.insert(std::pair<int, int>(rand()%25, rand()%50));
		}
		for_each(mp.begin(), mp.end(), print1);


		IntIntMap::iterator it = mp.lower_bound(11);
		if (it != mp.end()) std::cout << "I find it! " << it->first <<" " << it->second << std::endl;
		else std::cout << "failed!" << std::endl;
		IntIntMap::iterator it1 = mp.upper_bound(11);
		if (it1 != mp.end()) std::cout << "I find it! " << it1->first <<" " << it1->second  << std::endl;
		else std::cout << "failed!" << std::endl;


		std::pair<IntIntMap::iterator, IntIntMap::iterator> It = mp.equal_range(10);
		IntIntMap::iterator x = It.first;
		std::cout << x->first  <<" " << x->second  << std::endl;
		x = It.second;
		std::cout << x->first  <<" " << x->second  << std::endl;




		for (IntIntMap::iterator x = mp.begin(); x != mp.end(); )
		{
			if (x->second == 11)
			{
				//mp.erase(x);//runtime error
				mp.erase(x++);
			}
			else
			{
				++x;
			}
		}


		for_each(mp.begin(), mp.end(), print1);
	}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值