STL map

  由于对一道题目的错误理解,导致突然萌发了对与map的用法的开发,就是在形如 map< Node, int >,那么只要在重载Node的 '<'操作符时只针对个别元素,那么这些特别元素相同的结构体会被map视为相同的。

  代码如下:

#include <map>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

struct Node
{
	int a, b;
	string str;
    Node( int n, int m, string x )
	{
		a= n, b= m;
		str= x;
	}
	Node(){}
	bool operator < ( const Node &t ) const
	{
		return str< t.str;
	}
}a( 1, 2, "abc" ), b( 123, 312, "abc" );

int main()
{
	map< Node, int >mp;
	mp[ a ]= 1;
	if( mp.count( b ) )
	{
		printf( "相同\n" );
	}
	else
	{
		printf( "不祥同\n" );
	}
}
posted @ 2011-08-08 22:26  沐阳  阅读(239)  评论(0编辑  收藏  举报