C++ c+中的多集+;哎呀
从person类开始,创建一个多集来保存指向person的指针 物体。使用comparePersons函数对象定义multiset,以便 按人名自动排序。定义六个人,把 在“multiset”中显示它们,并显示其内容。其中几个人应该 具有相同的名称,以验证multiset是否使用 同一把钥匙 问题:我无法对多集进行排序,我不确定原因 代码: //对指针存储的person对象进行排序C++ c+中的多集+;哎呀,c++,c++14,C++,C++14,从person类开始,创建一个多集来保存指向person的指针 物体。使用comparePersons函数对象定义multiset,以便 按人名自动排序。定义六个人,把 在“multiset”中显示它们,并显示其内容。其中几个人应该 具有相同的名称,以验证multiset是否使用 同一把钥匙 问题:我无法对多集进行排序,我不确定原因 代码: //对指针存储的person对象进行排序 #include <iostream> #include <algorithm> #inc
#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()
#包括
#包括
#包括
#包括//添加---------------------------------------------
使用名称空间std;
班主任
{
私人:
字符串lastName;
字符串名;
长电话号码;
公众:
//默认构造函数
person():lastName(“空白”)、firstName(“空白”)、电话号码(0L)
{ }
//3-arg构造函数
人物(弦乐拉娜、弦乐国际泳联、长笛):
姓(拉娜)、名(国际泳联)、电话号码(pho)
{ }
friend bool运算符您的比较器应为多集类型:
multiset<person*, comparePersons> multiPtrsPers;
multiset-multitrspers;
(多)集合不能通过以后的其他比较重新排序。
您不能在该容器上调用sort
。集合不能sort
ed,因为它们使用内部排序来快速定位和插入元素。对集合进行外部排序会破坏这一点
相反,如果您参考,您会发现您可以自己调整排序:
template<
class Key,
class Compare = std::less<Key>,
class Allocator = std::allocator<Key>
> class multiset;
模板<
类密钥,
类比较=标准::更少,
类分配器=std::分配器
>类多集;
这意味着你想要的是:
std::multiset<person*, comparePersons> multiPtrsPers;
std::multiset multitrspers;
请不要在垃圾邮件标签上使用。当你绑定到那个标准时,你只需要标记一个C++版本的特定版本(它不是当前的一个)。你使用的是哪个版本的C++?C++ 14,我想尽快解决我的查询。tagged@abcd:添加更多标签并不一定能让你更快地回答问题。“我想尽快解决我的问题,这就是我标记的原因”…任何问题都会尽快得到回答,垃圾标签在这方面没有帮助。但是这个问题也不会像任何其他问题一样尽快得到回答…”我想尽快解决我的问题,这就是我标记的原因“-您的紧迫性与我们无关,也与问题的未来可行性/相关性无关。C:\Program Files\CodeBlocks\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\C++\bits\stl_algo.h | 1969 |错误:“运算符-”不匹配(操作数类型为“std:”Rb_tree_const_迭代器“和”std:”Rb_tree_const|const|iterator“迭代器”)|现在出现此错误请阅读完整答案,您无法调用std::sort
。