#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
class person{
public:
person(string name,int age){
this->m_name=name;
this->m_age=age;
}
person(const person &p){
this->m_name=p.m_name;
this->m_age=p.m_age;
}
bool operator<(const person&p){
return this->m_age<p.m_age;
}
string m_name;
int m_age;
};
class greaterage{
public:
bool operator()(const person &p1,const person&p2){
return p1.m_age>p2.m_age;
}
};
void show(const person &p){
cout<<"姓名 "<<p.m_name<<"年齡 "<<p.m_age<<endl;
}
void test(){
vector<person>p;
person p1("張三",24);
person p2("王二",27);
person p3("趙四",22);
person p4("劉一",25);
person p5("朱八",23);
p.push_back(p1);
p.push_back(p2);
p.push_back(p3);
p.push_back(p4);
p.push_back(p5);
vector<person>v;
person v1("朱八",23);
person v2("王二",27);
person v3("張三",24);
person v4("王二",25);
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
v.push_back(v4);
sort(v.begin(),v.end(),greaterage());
sort(p.begin(),p.end(),greaterage());
vector<person>t;
t.resize(v.size()); //這步編譯器報錯no match function for call to 'person::person()'
set_intersection(v.begin(),v.end(),p.begin(),p.end(),t.begin());
for_each(t.begin(),t.end(),show);
}
int main(){
test();
return 0;
}
uj5u.com熱心網友回復:
set_intersection 應該要多載‘==’吧 ,不過resize()那里一直報錯,我換reserve()的話都不報錯,轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/218850.html
標籤:C++ 語言
上一篇:虛函式&虛繼承
