【问题标题】:Multi set in c++ and oopsC++ 和 oops 中的多组
【发布时间】:2020-04-20 17:48:52
【问题描述】:

从 person 类开始,创建一个 multiset 来保存指向 person 的指针 对象。使用 comparePersons 函数对象定义多重集,因此它将 按人名自动排序。定义六个人,把 它们在多重集中,并显示其内容。几个人应该 具有相同的名称,以验证多重集存储多个对象 相同的键。

问题:我无法对多重集进行排序,我不知道为什么。

代码: // 对指针存储的人对象进行排序

#include <iostream>
#include <algorithm>
#include <string>
#include <set> //addded---------------------------------------------
using namespace std;

class person
{
private:
   string lastName;
   string firstName;
   long phoneNumber;
public:
   // default constructor
   person() :lastName("blank"), firstName("blank"), phoneNumber(0L)
   { }
   // 3-arg constructor
   person(string lana, string fina, long pho) :
       lastName(lana), firstName(fina), phoneNumber(pho)
   { }
   friend bool operator<(const person&, const person&);
   friend bool operator==(const person&, const person&);

   void display() const // display person's data
   {
       cout << endl << lastName << ",\t" << firstName
           << "\t\tPhone: " << phoneNumber;
   }
   long get_phone() const // return phone number
   {
       return phoneNumber;
   }
}; //end class person
//--------------------------------------------------------------
// overloaded < for person class
bool operator<(const person& p1, const person& p2)
{
   if (p1.lastName == p2.lastName)
       return (p1.firstName < p2.firstName) ? true : false;
   return (p1.lastName < p2.lastName) ? true : false;
}
//--------------------------------------------------------------
// overloaded == for person class
bool operator==(const person& p1, const person& p2)
{
   return (p1.lastName == p2.lastName &&
       p1.firstName == p2.firstName) ? true : false;
}
//--------------------------------------------------------------
// function object to compare persons using pointers
class comparePersons
{
public:
   bool operator() (const person* ptrP1, const person* ptrP2) const
   {
       return *ptrP1 < *ptrP2;
   }
};
//--------------------------------------------------------------
//function object to display a person, using a pointer
class displayPerson
{
public:
   void operator() (const person* ptrP) const
   {
       ptrP->display();
   }
};
////////////////////////////////////////////////////////////////
int main()
{   
   //make persons
   person* ptrP1 = new person("KuangThu", "Bruce", 4157300);
   person* ptrP2 = new person("Deauville", "William", 8435150);
   person* ptrP3 = new person("Wellington", "John", 9207404);
   person* ptrP4 = new person("Bartoski", "Peter", 6946473);
   person* ptrP5 = new person("Fredericks", "Roger", 7049982);
   person* ptrP6 = new person("McDonald", "Stacey", 7764987);
   person* ptrP7 = new person("KuangThu", "Bruce", 4157300);
   person* ptrP8 = new person("Deauville", "William", 8435150);

   //Creating multiset
   multiset<person*> multiPtrsPers;
   multiPtrsPers.insert(ptrP1);
   multiPtrsPers.insert(ptrP2);
   multiPtrsPers.insert(ptrP3);
   multiPtrsPers.insert(ptrP4);
   multiPtrsPers.insert(ptrP5);
   multiPtrsPers.insert(ptrP6);
   multiPtrsPers.insert(ptrP7);
   multiPtrsPers.insert(ptrP8);

   for_each(multiPtrsPers.begin(),multiPtrsPers.end(), displayPerson());//display person
   cout << endl;

   sort(multiPtrsPers.begin(), multiPtrsPers.end(), comparePersons());//compare persons

   for_each(multiPtrsPers.begin(), multiPtrsPers.end(), displayPerson());//display persons
   cout << endl;

   system("pause");
   return 0;
} end main()

【问题讨论】:

  • 请不要发送垃圾标签。当您绑定到该标准时,您只需要标记特定版本的 c++ 标准(并且它不是当前标准)。您使用的是哪个版本的 c++?
  • c++14 我想尽快解决我的查询,这就是我标记的原因
  • @abcd:添加更多标签并不一定能让你更快地得到答案。
  • “我想尽快解决我的查询,这就是我标记的原因”......任何问题都会尽快得到回答,垃圾邮件标签对此无济于事。但是这个问题也没有像其他任何问题一样尽快得到回答......
  • “我想尽快解决我的查询,这就是我标记的原因” - 您的紧迫性我们关心,也不关心该问题的未来可行性/相关性。

标签: c++ c++14


【解决方案1】:

你的比较器应该是多集类型:

multiset<person*, comparePersons> multiPtrsPers;

Demo

(multi)set 之后无法通过其他比较重新排序。
您不能在该容器上调用 sort

【讨论】:

  • C:\Program Files\CodeBlocks\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\bits\stl_algo.h|1969|错误:不匹配' operator-' (操作数类型为 'std::_Rb_tree_const_iterator' 和 'std::_Rb_tree_const_iterator')|现在收到此错误
  • 阅读完整答案,不能拨打std::sort
【解决方案2】:

集合不能sorted,因为它们使用内部排序来快速定位和插入元素。在外部对集合进行排序会破坏这一点。

相反,如果你参考the documentation,你会发现你可以自己调整排序:

template<
    class Key,
    class Compare = std::less<Key>,
    class Allocator = std::allocator<Key>
> class multiset;

这意味着你想要的是:

std::multiset<person*, comparePersons> multiPtrsPers;

【讨论】:

    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-11-03
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多