set 和比较/排序函子或 less 运算符

问题描述 投票:0回答:2

我的设置有问题。我不知道我做错了什么。也许你们中的某个人可以帮助我。那么让我们开始吧,我的程序的输出应该是:

Iksinski Adam, Kowalski Jan, Nowak Adam, Nowak Jan,

所以它按第一个字符串排序。

这是我的程序:

#include <set>
#include <iterator>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
class Person{
public:
Person(){}
Person(string v , string v1):nazw(v),imie(v1){}
bool operator<(const Person & K) const
{
    return ((this->getN()>K.getN())?0:1);
    //return ((this->getN()<K.getN())?1:0);
}
string getN()const
{
    return nazw;
}
/*
bool operator()(Person & K, Person & K1)
{
    return ((K->getN()<K1.getN())?1:0);
}
*/
friend ostream& operator<<(ostream & o , const Person&K)
{
    o << K.nazw << " " << K.imie;
    return o;
}
private:
string nazw,imie;
};
struct cmp
{
    bool operator()(const Person &K , const Person &K1)
    {
        return ((K.getN()<K.getN())?1:0);
    }
};
int main()
{
//typedef set<Person> kontener_typ;
typedef set<Person,cmp> kontener_typ;
kontener_typ c;
c.insert(Person("Nowak","Jan"));
c.insert(Person("Nowak","Adam"));
c.insert(Person("Kowalski","Jan"));
c.insert(Person("Nowak","Adam"));
c.insert(Person("Iksinski","Adam"));
std::copy (c.begin(), c.end(), ostream_iterator<Person>(cout, " ,"));
std::cout << std::endl;
}

好的,所以在 main 中我只能编辑typdef和复制函数(但我需要使用它来输出集合)。 就像你看到的,我试图重载运算符< in Person (because set compering Person to Person) but it doeasnt work . I also trying with functor but it then the output looks like

Iksinski Adam ,Nowak Adam ,Kowalski Jan ,Nowak Adam ,Nowak Jan ,

所以第二个字符串应该被删除。

祝你好运:)。

c++ stl set functor
2个回答
3
投票

您的代码正在使用比较器

cmp
函子对象。其中有一个错误:

struct cmp
{
    bool operator()(const Person &K , const Person &K1)
    {
        // one of these _should_ be K1
        return ((K.getN()<K.getN())?1:0);
    }
};

我喜欢以一种清晰的方式命名我的变量,如何比较它们,例如:

struct cmp
{
    bool operator()(const Person &left , const Person &right)
    {
        return left.getN() < right.getN();
    }
};

这澄清了(至少对我来说)操作员正在进行这样的比较:

left < right

但是,您还需要按“名字”作为次要条件进行排序,这将使函数看起来像这样:

struct cmp
{
    bool operator()(const Person &left , const Person &right)
    {
        if(left.getN() < right.getN())
           return true;
        else if(left.getN() > right.getN())
           return false;
        // assuming the getI() function returns the first name,
        // just as the getN() function returns the last name
        else if(left.getI() < right.getI())  
           return true;
        else
           return false;
    }
};

0
投票

您必须按姓氏和名字进行比较:

bool operator<(const Person & other) const
{
    if ( getN() < other.getN() )
      return true;
    if ( other.getN() < getN() )
      return false;
    if ( imie < other.imie() )
      return true;
    return false;
}

或者,如果您想使用

struct cmp

struct cmp
{
    bool operator()(const Person &K , const Person &K1)
    {
      if (K.getN() < K1.getN())
        return true;
      if(K1.getN() < K.getN())
        return false;
      if(K.imie < K1.imie) // will need a friend declaration or a getter() func
        return true;
      return false;
    }
}

如果您有 C++ 的

std::tie
,那么任一函数的内部都会变得简单得多:

return std::tie(nazw, imie) < std::tie(other.nazw, other.imie);
return std::tie(K.nazw, K.imie) < std::tie(K1.nazw, K1.imie);
© www.soinside.com 2019 - 2024. All rights reserved.