【第10章 关联容器】多关键字Map_Set实例

(1)排序准则

less返回值是不能含糊的,即:当less(a,b)=true时,less(b,a)一定要为false,
否则这两个节点就会被当成相等的节点。
【map相等性比较】两次调用less实现:less( a, b )和less( b, a),两者都为false则为相等。

(2)find准则
map在插入、查找、排序时,实际调用<操作符(两次调用),并不使用==操作符。


#if 1
// 多值组合作为key的情形
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>

using namespace std;

//template<typename int>
class key{
	friend class equal;
	friend bool operator<( const key&, const key& );
	friend bool operator==( const key&, const key& );
	friend ostream& operator<<( ostream&, const key& );
public:
	key( int x, int y ) : m_eyesight(x), m_height(y){}
private:
	int m_eyesight;
	int m_height;
};

// less返回值是不能含糊的,即:当less(a,b)=true时,less(b,a)一定要为false,
// 否则这两个节点就会被当成相等的节点。
// 【map相等性比较】两次调用less实现:less( a, b )和less( b, a),两者都为false则为相等。
// map在插入、查找、排序时,实际调用<操作符,并不使用==操作符
bool operator<( const key& kL, const key& kR )  
{  
	//按eyesight升序 + height升序排列
	if( kL.m_eyesight != kR.m_eyesight ){
		return kL.m_eyesight < kR.m_eyesight;
	}else{
		// 写错的地方:kL.m_height < kL.m_height;2014-6-12 16:12:11
		return kL.m_height < kR.m_height;
	}
}
bool operator==( const key& kl, const key& kr )
{
	if( kl.m_eyesight == kr.m_eyesight &&
		kl.m_height == kr.m_height )
		return true;
	else
		return false;
}

struct equal{
	bool operator()( const key& kl, const key& kr )
	{
		return kl.m_height == kr.m_height;
	}
};


ostream& operator<<( ostream& os, const key& k)
{
	os << k.m_eyesight << "\t" << k.m_height;
	return os;
}
int main( void )
{
	map<key, string> mapKey;
	
	mapKey.insert( make_pair( key( 3, 1 ), "ddd" ) );
	mapKey.insert( make_pair( key( 1, 1 ), "aaa" ) );
	mapKey.insert( make_pair( key( 2, 1 ), "bbb" ) );
	mapKey.insert( make_pair( key( 2, 2 ), "ccc" ) ); 

	map<key, string>::const_iterator it;
	for( it = mapKey.begin(); it != mapKey.end(); ++it )
		cout << it->first << "\t" << it->second << endl;

	it = mapKey.find( key( 2, 1 ) );
	cout << it->first << "\t" << it->second << endl;

	return 0;
}
#elif 0
// 单值作为key的情形
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <utility>

using namespace std;

class Student{
	friend ostream& operator<<( ostream& os, const Student& stu );
public:
	Student(int id, string name, float eyesight, float height ) :
		m_id( id ), m_name( name ), m_eyesight( eyesight ), m_height( height ){}
private:
	int m_id;		// 学号
	string m_name;	// 姓名
	float m_eyesight;// 视力
	float m_height;	// 身高
};

ostream& operator<<( ostream& os, const Student& stu )
{
	os << stu.m_id << "\int" << stu.m_name << "\int" << stu.m_eyesight << "\int" << stu.m_height;
	return os;
}

int main( void )
{
	cout << "be positive..." << endl;

	map<int, Student> stuMap;
	stuMap.insert( make_pair( 3, Student( 3, "Dudley", 1.1f, 163.4f ) ) );
	stuMap.insert( make_pair( 1, Student( 1, "Bob",1.1f, 170.2f ) ) );
	stuMap.insert( make_pair( 2, Student( 2, "Chris", 1.5f, 166.6f ) ) );
	
	map<int, Student>::const_iterator it = stuMap.begin();

	for( ; it != stuMap.end(); ++it ){
		cout << it->second << endl;
	}

	return 0;
}
#endif


前四行为插入直接输出结果,最后一行为查找结果——



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值