Sorting 如何按第一个和第二个参数对std::map进行排序?

Sorting 如何按第一个和第二个参数对std::map进行排序?,sorting,map,Sorting,Map,这是我的std::map示例,比如std::mapmy\u map // ABC | aaa ABC | aaa // DEF | def ABC | dcd // BCD | def -> ABC | zzz // DEF | bcd BCD | def // ABC | dcd DEF | bcd // ABC | zzz DEF | def 如您所见,我正在尝试对左std::map进行排序,然后得到右一个

这是我的
std::map
示例,比如
std::mapmy\u map

//   ABC | aaa      ABC | aaa
//   DEF | def      ABC | dcd
//   BCD | def  ->  ABC | zzz
//   DEF | bcd      BCD | def
//   ABC | dcd      DEF | bcd
//   ABC | zzz      DEF | def
如您所见,我正在尝试对左
std::map
进行排序,然后得到右一个

这是我的代码(我使用的不是字符串,而是我的自定义类型。无论如何,最后,我对字符串进行排序):

第一列中的所有数据都已排序,但第二列没有排序(当然,因为我们只在第一列使用!)

镜像的情况,当我只使用

return (*_left.second).name() < (*_right.second).name();
return(*_left.second).name()<(*_right.second).name();
第二列已排序

但是我需要立即对第一列和第二列进行排序。如何编写此代码?我做错了什么

谢谢你的帮助


抱歉,忘记此代码:

std::vector< std::pair< CompanyPtr, ContractorPtr > > n_map_( buddies_ccm_.begin(), buddies_ccm_.end() );
std::sort( n_map_.begin(), n_map_.end(), less_second< CompanyPtr, ContractorPtr >() );
std::vector>n_map_(buddies_ccm_.begin(),buddies_ccm_.end());
std::sort(n_map.begin(),n_map.end(),更短的时间());

只有当
\u left->first
\u right->first
相等时,才需要考虑第二列。换句话说,你想让你的代码首先比较
第一个
字段,只有当
第一个
比较不确定时(当两个
第一个
都相等时),才回头比较
第二个
字段。你说的是关键和价值观吗

std::map始终按键排序。可以在构建地图时指定比较对象来定义该顺序。但是这个比较对象并不比较
std::pair
s,而是比较映射的键类型的对象

此外,地图中的键是唯一的。因此,地图中不能有两个键为“ABC”的条目


我想您尝试使用
中的
std::sort
对地图进行排序。我不确定在这种情况下会发生什么,但我认为这不是您期望的情况。

使用
std::map
无法实现您想要的,因为它已经在另一个答案中解释过。如果您的配对没有重复,您可以使用一组配对:

std::set<std::pair<std::string, std::string> > pair_set;
std::set pair\u set;

您不需要提供比较器,因为
std::pair
已经支持您需要的比较类型。

您的比较函数是错误的。当两个
first.name()
相等时,它不起作用。试着这样做:

 bool operator ()( type const& _left, type const& _right ) const
 {
     if ((*_left.first).name() > (*_right.first).name())
        return false;
     if ((*_left.first).name() < (*_right.first).name())
        return true;
     return ( (*_left.second).name() < (*_right.second).name() );
 }
bool运算符()(类型const&_left,类型const&_right)const
{
如果((*_left.first).name()>(*_right.first.name())
返回false;
如果((*_left.first).name()<(*_right.first).name())
返回true;
返回((*_left.second).name()<(*_right.second).name());
}

这是家庭作业吗?如果是,请添加“家庭作业”标签。@jeremytrimble不,不是!对于家庭作业,我不仅向您展示了问题,还展示了整个问题(代码、想法等)。关于编辑:您是否尝试对
std::map
std::vector
进行排序?@Stephan我们无法对
std::map
进行简单排序,对吗?因为,键已使用标准的less函子排序。因此,这里我尝试使用
std::vector
对映射进行排序,使用我预定义的functor
less_second
..“你在谈论键和值吗?”是的,当然是键/值。在
std::map
中,不仅是字符串,而且是我的自定义数据类型。我想当我尝试这样比较时:
(*_left.first).name()<(*_right.first.name())&(*_left.second).name()<(*_right.second.name())
比较方法变得“不稳定”,因为当第一个“column”-键排序时,所有数据表都会更改,但我尝试对第二个“column”-值排序,结果不正确…不,我必须编写自己的comparator函子,因为它不是一个简单的字符串,而是我自己的自定义数据类型(带有字符串、整数和其他数据)…是的!!!这是正确的答案,
std::vector
现在按我想要的排序!非常感谢,斯蒂芬。
std::set<std::pair<std::string, std::string> > pair_set;
 bool operator ()( type const& _left, type const& _right ) const
 {
     if ((*_left.first).name() > (*_right.first).name())
        return false;
     if ((*_left.first).name() < (*_right.first).name())
        return true;
     return ( (*_left.second).name() < (*_right.second).name() );
 }