C++ 如何在c+中使用两个排序条件(对于一组对)创建一个排序集+;?

C++ 如何在c+中使用两个排序条件(对于一组对)创建一个排序集+;?,c++,C++,我需要对一组对进行排序(一个是int,第二个是char),我需要对我的集进行如下排序: 12G、11F、10A、10B、10C(第一个按降序排列,第二个按升序排列) 首先。这就是我迄今为止所尝试的,我得到了一些错误: #include <iostream> #include <fstream> #include <algorithm> #include <utility> #include <set> using namespace

我需要对一组对进行排序(一个是int,第二个是char),我需要对我的集进行如下排序: 12G、11F、10A、10B、10C(第一个按降序排列,第二个按升序排列) 首先。这就是我迄今为止所尝试的,我得到了一些错误:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <utility>
#include <set>

using namespace std;
set <pair <int,char> > s;

bool myfunction( const pair<int, char>& i, const pair<int, char>& j ) {
    if( i.first < j.first ) return false;
    if( j.first < i.first ) return true;
    return j.second < i.second;
}

void writes()
{   set <pair<int,char> >::iterator it;
    for (it = s.begin();it<= s.end();it++) /// line (18)
        cout<<(*it).second<<" "<<(*it).first<<"\n\n";
}
int main()
{   ifstream f("info.in");
    int n;
    f>>n;
    for (int i=1;i<=n;i++)
    {   pair<int,char> x;
        int st;
        char nd;
        f>>st;
        f>>nd;
        x.first=st;
        x.second=nd;
        s.insert(x);
    }
    writes();
}
@Sam Varshavchik,谢谢!这解决了我的错误问题。 但是,我仍然没有得到我需要的输出。 我只得到:

10 A
10 B
10 C
11 F
12 G
是否可以成对更改订单条件?如果不是,您建议使用什么替代


看起来程序仍然忽略了排序条件的myfunction。我怎么能把它塞进我的鞋子里?看起来,它只是放在那里,从来没用过。不管怎样,程序都能完成它的工作

我也试过: 但它仍然不起作用

using namespace std;

struct lex_compare {
    bool operator()(const pair<int, char>& i, const pair<int, char>& j )
{
   if( i.first != j.first )
   {
      return (i.first > j.first);
   }

   return (j.second > i.second);
}
} // forgot ";", after adding it, it works perfectly.
set <pair <int,char>, lex_compare > s; ///line (22)

void writes()
{   set <pair<int,char> >::iterator it;
    for (it = s.begin();it!= s.end();it++) /// line (18)
        cout<<(*it).second<<" "<<(*it).first<<"\n\n";
}
int main()
{   ifstream f("info.in");
    int n;
    f>>n;
    for (int i=1;i<=n;i++)
    {   pair<int,char> x;
        int st;
        char nd;
        f>>st;
        f>>nd;
        x.first=st;
        x.second=nd;
        s.insert(x);
    }
    writes();
}
使用名称空间std;
结构lex_比较{
布尔运算符()(常数对&i,常数对&j)
{
如果(i.first!=j.first)
{
返回(i.first>j.first);
}
返回(j秒>i秒);
}
}//忘了“;”,添加后,效果非常好。
设置s///第(22)行
void写入()
{set::迭代器;
for(it=s.begin();it!=s.end();it++)///行(18)
库特
(只有随机访问迭代器可以使用
运算符安全地进行比较,
std::set
s迭代器不是随机访问迭代器)

这回答了您提出的问题:编译错误。这个问题实际上与您的自定义集比较函数无关;这将是一个不同的问题。

基于

第一个按降序排列,第二个按升序排列

比较功能需要:

bool myfunction( const pair<int, char>& i, const pair<int, char>& j )
{
   if( i.first != j.first )
   {
      return (i.first > j.first);
   }

   return (j.second < i.second);
}
set <pair<int,char>, mycompare >::iterator it;
使用

这需要:

bool myfunction( const pair<int, char>& i, const pair<int, char>& j )
{
   if( i.first != j.first )
   {
      return (i.first > j.first);
   }

   return (j.second < i.second);
}
set <pair<int,char>, mycompare >::iterator it;

您永远不会调用
myfunction
或使用它做任何事情

要使用它对集合进行排序,请将
myfunction
转换为函子,如下所示:

5
10 B
10 A
10 C
11 F
12 G
struct comparepair {
    bool operator()( const pair<int, char>& i, const pair<int, char>& j ) {
        if( i.first < j.first ) return false;
        if( j.first < i.first ) return true;
        return i.second < j.second;
    }
};
struct comparepair{
布尔运算符()(常数对&i,常数对&j){
如果(i.first
然后将集合声明为比较器,如

set <pair <int,char>, comparepair > s;
set;

为了使用自定义比较器,您必须告诉容器有关函数/函子的信息,如上面链接的SO问题所述

下面是一个适用于您的案例的工作示例

#include <iostream>
#include <set>

typedef std::pair<int, char> pic_t;


struct comp {
    bool operator () ( const pic_t& p1, const pic_t& p2 ) const {
        return ( p1.first != p2.first ) ? ( p1.first > p2.first ) : ( p1.second < p2.second );
        //  identical, slightly better performance:
        //  return ( p1.first > p2.first ) || ( ! ( p2.first > p1.first ) && ( p1.second < p2.second ) );
    }
};


int main()
{
    std::set<pic_t, comp> s = { { 10, 'b' }, { 10, 'a' }, { 10, 'c' }, { 11, 'f' }, { 12, 'g' } };

    for ( auto p : s )
        std::cout << p.first << ", " << p.second << std::endl;

    return 1;
}
#包括
#包括
typedef std::成对图片;
结构组件{
布尔运算符()(常数pic\u t&p1,常数pic\u t&p2)常数{
返回(p1.first!=p2.first)?(p1.first>p2.first):(p1.secondp2.first)| |(!(p2.first>p1.first)和&(p1.secondstd::难道程序仍然忽略了排序条件的myfunction。我如何在我的配对中重载它?看起来,它就在那里,而且从未使用过。程序不管它如何工作,在添加它之后,它工作得很好。
根据一个简单的印刷错误投票关闭。
void writes()
{
   auto it = s.begin();
   for ( ; it != s.end(); it++)
   {
      cout<<(*it).second<<" "<<(*it).first<<"\n\n";
   }
}
struct comparepair {
    bool operator()( const pair<int, char>& i, const pair<int, char>& j ) {
        if( i.first < j.first ) return false;
        if( j.first < i.first ) return true;
        return i.second < j.second;
    }
};
set <pair <int,char>, comparepair > s;
#include <iostream>
#include <set>

typedef std::pair<int, char> pic_t;


struct comp {
    bool operator () ( const pic_t& p1, const pic_t& p2 ) const {
        return ( p1.first != p2.first ) ? ( p1.first > p2.first ) : ( p1.second < p2.second );
        //  identical, slightly better performance:
        //  return ( p1.first > p2.first ) || ( ! ( p2.first > p1.first ) && ( p1.second < p2.second ) );
    }
};


int main()
{
    std::set<pic_t, comp> s = { { 10, 'b' }, { 10, 'a' }, { 10, 'c' }, { 11, 'f' }, { 12, 'g' } };

    for ( auto p : s )
        std::cout << p.first << ", " << p.second << std::endl;

    return 1;
}