【问题标题】:how to create an ordered set using two ordering criteria(for a set of pairs) in c++?如何在 C++ 中使用两个排序标准(对于一组对)创建一个有序集?
【发布时间】:2016-05-31 00:50:50
【问题描述】:

我需要订购一组对(一个是 int,第二个是 char),我需要像这样订购我的集合: 12 G, 11 F, 10 A, 10 B, 10 C(降序排列第一个,升序排列第二个) 第一。这是我迄今为止尝试过的,我得到了一些错误:

#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();
}

我得到的第一个错误在第 (18) 行:'operator::.....

非常感谢您的帮助

我的输入文件如下所示:

5
10 B
10 A
10 C
11 F
12 G

@Sam Varshavchik,谢谢!这解决了我的错误问题。 但是,我仍然没有得到我需要的输出。 我只得到:

10 A
10 B
10 C
11 F
12 G

是否可以成对更改订单条件?如果没有,您会建议改用什么?

看起来排序标准的 myfunction 仍然被程序忽略。我怎么能在我的配对中超载它?看起来,它只是坐在那里,从未使用过。无论如何,该程序都可以正常工作

我也试过这个:Using custom std::set comparator 但是还是不行

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();
}

错误:第 (22) 行:'s' 之前的声明符无效;

【问题讨论】:

  • // forgot ";", after adding it, it works perfectly. 基于一个简单的印刷错误投票结束。

标签: c++


【解决方案1】:
for (it = s.begin();it<= s.end();it++)

迭代器通常不会实现小于/大于类型的比较。迭代器通常只实现==!= 比较,测试是否相等。这应该是:

for (it = s.begin();it != s.end();it++)

(只有随机访问迭代器可以安全地使用&lt;&gt; 运算符进行比较,并且std::sets 迭代器不是随机访问迭代器)

这回答了您提出的问题:编译错误。这个问题与您的自定义设置比较功能没有任何关系;这将是一个不同的问题。

【讨论】:

    【解决方案2】:

    你永远不会打电话给myfunction 或用它做任何事情!

    要使用它来订购您的集合,请将myfunction 转换为仿函数,如下所示:

    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;
    

    【讨论】:

      【解决方案3】:

      为了使用自定义比较器,你必须告诉容器你上面链接的 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;
      }
      

      【讨论】:

        【解决方案4】:

        基于

        先降序,后升序

        比较函数需要是:

        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 时使用它。而不是使用

        set <pair <int,char> > s;
        

        使用

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

        这将需要对您的程序进行更多更改。

        1. 您需要先声明/定义mycompare,然后再定义s
        2. 迭代器类型也需要改变。你有

          set <pair<int,char> >::iterator it;
          

          这需要:

          set <pair<int,char>, mycompare >::iterator it;
          

          如果你使用的编译器支持 C++11,你也可以使用auto

          void writes()
          {
             auto it = s.begin();
             for ( ; it != s.end(); it++)
             {
                cout<<(*it).second<<" "<<(*it).first<<"\n\n";
             }
          }
          

        【讨论】:

        • 看起来 myfunction 的排序标准仍然被程序忽略。我怎么能在我的配对中超载它?看起来,它就在那里,从未使用过。无论如何,该程序都可以正常工作
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多