下面是map定义的结构:

// TEMPLATE CLASS map
template<class _Kty,
class _Ty,
class _Pr = less<_Kty>,
class _Alloc = allocator<pair<const _Kty, _Ty> > >
class map
: public _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> >
{ // ordered red-black tree of {key, mapped} values, unique keys
public:
typedef map<_Kty, _Ty, _Pr, _Alloc> _Myt;
typedef _Tree<_Tmap_traits<_Kty, _Ty, _Pr, _Alloc, false> > _Mybase;
typedef _Kty key_type;
typedef _Ty mapped_type;
typedef _Ty referent_type; // retained
typedef _Pr key_compare;
typedef typename _Mybase::value_compare value_compare;
typedef typename _Mybase::allocator_type allocator_type;
typedef typename _Mybase::size_type size_type;
typedef typename _Mybase::difference_type difference_type;
typedef typename _Mybase::pointer pointer;
typedef typename _Mybase::const_pointer const_pointer;
typedef typename _Mybase::reference reference;
typedef typename _Mybase::const_reference const_reference;
typedef typename _Mybase::iterator iterator;
typedef typename _Mybase::const_iterator const_iterator;
typedef typename _Mybase::reverse_iterator reverse_iterator;
typedef typename _Mybase::const_reverse_iterator
const_reverse_iterator;
typedef typename _Mybase::value_type value_type; map()
: _Mybase(key_compare(), allocator_type())
{ // construct empty map from defaults
} explicit map(const key_compare& _Pred)
: _Mybase(_Pred, allocator_type())
{ // construct empty map from comparator
} map(const key_compare& _Pred, const allocator_type& _Al)
: _Mybase(_Pred, _Al)
{ // construct empty map from comparator and allocator
} template<class _Iter>
map(_Iter _First, _Iter _Last)
: _Mybase(key_compare(), allocator_type())
{ // construct map from [_First, _Last), defaults
_DEBUG_RANGE(_First, _Last);
for (; _First != _Last; ++_First)
this->insert(*_First);
} template<class _Iter>
map(_Iter _First, _Iter _Last,
const key_compare& _Pred)
: _Mybase(_Pred, allocator_type())
{ // construct map from [_First, _Last), comparator
_DEBUG_RANGE(_First, _Last);
for (; _First != _Last; ++_First)
this->insert(*_First);
} template<class _Iter>
map(_Iter _First, _Iter _Last,
const key_compare& _Pred, const allocator_type& _Al)
: _Mybase(_Pred, _Al)
{ // construct map from [_First, _Last), comparator, and allocator
_DEBUG_RANGE(_First, _Last);
for (; _First != _Last; ++_First)
this->insert(*_First);
} #if _HAS_STRICT_CONFORMANCE
void erase(iterator _Where)
{ // erase element at _Where
_Mybase::erase(_Where);
} size_type erase(const key_type& _Keyval)
{ // erase and count all that match _Keyval
return (_Mybase::erase(_Keyval));
} void erase(iterator _First, iterator _Last)
{ // erase [_First, _Last)
_Mybase::erase(_First, _Last);
}
#endif /* _HAS_STRICT_CONFORMANCE */ mapped_type& operator[](const key_type& _Keyval)
{ // find element matching _Keyval or insert with default mapped
iterator _Where = this->lower_bound(_Keyval);
if (_Where == this->end()
|| this->comp(_Keyval, this->_Key(_Where._Mynode())))
_Where = this->insert(_Where,
value_type(_Keyval, mapped_type()));
return ((*_Where).second);
}
};

less的定义

template<class _Ty>
struct less
: public binary_function<_Ty, _Ty, bool>
{ // functor for operator<
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{ // apply operator< to operands
return (_Left < _Right);
}
};

从上面定义可以看出,map<_Kty, _Ty, _Pr, _Alloc>的后两个默认的参数,class _Pr = less<_Kty> , class _Alloc = allocator<pair<const _Kty, _Ty> > >;

而默认缺省定义map时,此时如果结构体为key值时,而此时class _Pr = less<_Kty>要进行_Left < _Right,而如果此时结构体没有<方法时,则会出错。

解决上面问题有两种方法:1.在结构体重重载<操作符;2.定义自己的排序函数,显式调用。

1.结构体中重载<

#include <iostream>
#include <string>
#include <map>
using namespace std;
struct StructTest
{
string sTemp;
int nCount;
bool operator<(const StructTest& test) const
{
if (sTemp < test.sTemp)
{
return true;
}
else if (sTemp == test.sTemp)
{
if (nCount < test.nCount)
return true;
else
return false;
}
else
{
return false;
}
}
};
void main()
{
map<StructTest,string>mapTest;
map<StructTest,string>::iterator mapTestiter;
StructTest StructTest1,StructTest2,StructTest3;
StructTest1.sTemp="111111";
StructTest1.nCount=6;
StructTest2.sTemp="111111";
StructTest2.nCount=7;
StructTest3.sTemp="000000";
StructTest3.nCount=6;
mapTest.insert(make_pair(StructTest1,"1"));
mapTest.insert(make_pair(StructTest2,"2"));
mapTest.insert(make_pair(StructTest3,"3"));
for (mapTestiter=mapTest.begin();mapTestiter!=mapTest.end();mapTestiter++)
{
cout<<(mapTestiter->first).sTemp<<" "<<(mapTestiter->first).nCount<<" "<<mapTestiter->second<<endl;
} }

2.定义自己的排序函数,显式调用

#include <iostream>
#include <string>
#include <map>
using namespace std;
struct StructTest
{
string sTemp;
int nCount;
}; struct ptr_less : public binary_function<StructTest,StructTest,bool>
{
bool operator()(const StructTest& test1, const StructTest& test2) const
{
if (test1.sTemp < test2.sTemp)
{
return true;
}
else if (test1.sTemp == test2.sTemp)
{
if (test1.nCount < test2.nCount)
return true;
else
return false;
}
else
{
return false;
}
}
}; void main()
{
map<StructTest,string,ptr_less>mapTest;
map<StructTest,string,ptr_less>::iterator mapTestiter;
StructTest StructTest1,StructTest2,StructTest3;
StructTest1.sTemp="111111";
StructTest1.nCount=6;
StructTest2.sTemp="111111";
StructTest2.nCount=7;
StructTest3.sTemp="000000";
StructTest3.nCount=6;
mapTest.insert(make_pair(StructTest1,"1"));
mapTest.insert(make_pair(StructTest2,"2"));
mapTest.insert(make_pair(StructTest3,"3"));
for (mapTestiter=mapTest.begin();mapTestiter!=mapTest.end();mapTestiter++)
{
cout<<(mapTestiter->first).sTemp<<" "<<(mapTestiter->first).nCount<<" "<<mapTestiter->second<<endl;
} }

当然其中的find方法也与_Pr有关。

map中find调用红黑树的find

具体调用如下:1.map中的find

2.红黑树中find

注意:STL中的SET与map一样。

具体调用可以查看STL源码剖析、或者vs中查看相关定义

STL map、set中key为结构体的用法的更多相关文章

  1. 剔除list中相同的结构体数据

    剔除list中相同的结构体数据,有三个思路:1.两层循环,逐个比较 2.使用set容器来剔除 3.使用unique方法去重 // deduplication.cpp : 定义控制台应用程序的入口点. ...

  2. MFC中的NMHDR结构体和NMUPDOWN结构体

    建立spin控件,创建UDN_DELTAPOS一个消息函数后: void CSpinDlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult) { NM ...

  3. 获取map集合中key、value

    获取Map集合类中key.value的两种方法 方法一:利用Set集合中的keySet()方法 Map<String,String> map = new HashMap<String ...

  4. C语言中 不定义结构体变量求成员大小

    所谓的求成员大小, 是求成员在该结构体中 用 sizeof(结构体名.结构体成员名) 求来的. 很多时候我们需要知道一个结构体成员中的某个成员的大小, 但是我们又不需要定义该结构体类型的变量(定义的话 ...

  5. 过滤掉map集合中key或value为空的值

    package cn.com.utils; import org.apache.commons.lang3.StringUtils; import java.util.Collection; impo ...

  6. 如何系统学习C 语言(中)之 结构体篇

    1,结构体 在前面我们知道变量和数组都可以用来存储数据,变量用来存储单个数据,数组可以用来存储一组同类型的数据,但你有没有发现--它们都只适合单一属性的数据.那现实生活中,很多对象都是具有多属性的.例 ...

  7. 在Main中定义student的结构体,进行年龄从大到小依次排序录入学生信息。(结构体的用法以及冒泡排序)

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  8. Linux中网络通信中 使用的结构体

    "+++++++++++++++++++++++++ Linux TCP/UDP通信中的结构体 +++++++++++++++++++++++++++++++++++++++" s ...

  9. 【2016-08-18】转载:总结C++中几种结构体初始化的方法

    作者:Ac_Von 博客地址:http://www.cnblogs.com/vongang/ 文章地址:http://www.cnblogs.com/vongang/archive/2011/07/3 ...

随机推荐

  1. Linux Shell常用命令(长期更新)

    #判断某个字段是否匹配指定值 awk -F"," '{if($4=="value"){print $1} else {print $0}}' file.txt ...

  2. tp3.2报错;syntax error, unexpected 'function' (T_FUNCTION), expecting identifier (T_STRING) or \\ (T_NS_SEPARATOR)

    出错原因:这个是php版本问题,laravel5.1的php版本要求是PHP >= 5.5.9,切换一下PHP版本就行.

  3. java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed

    错误描述: ElasticSearch集群启动错误,错误的原因是:因为Centos6不支持SecComp,而ES默认bootstrap.system_call_filter为true进行检测,所以导致 ...

  4. dns文件

    1.dns简介 dns为域名解析系统,当本地浏览器输入域名访问网站时,如果本地host中没有配置域名与IP的对应关系,那么域名信息将会被发送到dns服务器上,由dns服务器将域名解析为IP(过程较为复 ...

  5. STL——泛型编程

    1.指针的算术运算 对于一个存储int数据的vector,我们要查找某值是否存在于其中,采用下标操作的做法如下: int* find(const vector<int> &vec, ...

  6. red hat 7 启动过程(EFI)

    不同版本的Linux系统的启动过程在某些地方是不一样的,现在先来介绍一下red hat 7 的启动过程(EFI). (加电→图形登录界面) 接通电源 按下电源键 EFI固件启动 初始化硬件 从EFI启 ...

  7. 数据库 MySQL part1

    数据库介绍 数据库(database,DB)是指长期存储在计算机内的,有组织,可共享的数据的集合.数据库中的数据按一定的数学模型组织.描述和存储,具有较小的冗余,较高的数据独立性和易扩展性,并可为各种 ...

  8. TFS权限配置

            装了TFS,要给TFS里添加用户,然后分配权限.其实一般项目中权限都不会控制的那么细,所以就直接想给项目组的每个人建一个用户,让他们都能访问这个项目的代码并进行任何操作.只想怎么简单怎 ...

  9. 【WPF】 前言

    [WPF] 前言 前段时间项目中用到了WPF,就边学边做项目,一个项目做下来有点感触,以此记录. 以前也开发过多个C/S项目, 一直都是用的Winform,Winform 做些简单的界面很方便,基本只 ...

  10. springmvc基础篇—处理图片静态资源文件

    当我们在web.xml中对DispatcherServlet的过滤设置为/ 的时候,表示对所有的路径进行拦截过滤,那么不可避免的就会产生一个问题,那就是像图片这种静态资源文件我明明引用路径有,但就是加 ...