64,683
社区成员
发帖
与我相关
我的任务
分享
/**
* 比较器接口
*/
interface MyComp<T>{
int compare(T t1,T t2);
}
/**
*id策略
*
*/
class IdComp implements MyComp<Person>{
@Override
public int compare(Person t1, Person t2) {
return t1.id-t2.id;
}
}
/**
* age策略
*
*/
class AgeComp implements MyComp<Person>{
@Override
public int compare(Person t1, Person t2) {
return t1.age-t2.age;
}
}
class Person{
int id;
int age;
public Person(int id, int age) {
super();
this.id = id;
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", age=" + age + "]";
}
}
public class MpSort {
/**
* 交换
*/
public static <T> void swap(T[] array,int a,int b){
T temp=array[a];
array[a]=array[b];
array[b]=temp;
}
/**
* 输出array
*/
public static <T> void printArray(T[] array){
for(T t:array){
System.out.println(t);
}
}
/**
*泛型冒泡
*/
public static <T> void sort(T[] array,MyComp<T> comp){
for(int i=0;i<array.length-1;i++){
for(int j=0;j<array.length-1-i;j++){
if(comp.compare(array[j], array[j+1])>0){
swap(array, j, j+1);
}
}
}
}
public static void main(String[] args) {
Person p1=new Person(1, 3);
Person p2=new Person(2, 2);
Person p3=new Person(3, 1);
Person[] array={p2,p1,p3};
//第一次我想根据id排序
sort(array, new IdComp());
System.out.println("id升序");
printArray(array);
//第二次我想根据age排序
System.out.println("age升序");
sort(array, new AgeComp());
printArray(array);
}
}
#include <iostream>
#include<sstream>
using namespace std;
template<typename T>
/**
* 比较器接口
*/
class MyComp{
public:
int compare(T *t1,T *t2);
};
class Person{
public:
int id;
int age;
Person(int id, int age) {
this->id = id;
this->age = age;
}
string toString(){
ostringstream oss;
oss << "{age:" << this->age<<",id:" <<this->id<< "}"<<endl;
return oss.str();
}
};
/**
*id策略
*
*/
class IdComp: virtual public MyComp<Person>{
public:
int compare(Person *t1, Person *t2) {
return t1->id-t2->id;
}
};
/**
* age策略
*
*/
class AgeComp : virtual public MyComp<Person>{
public:
int compare(Person *t1, Person *t2) {
return t1->age-t2->age;
}
};
template<typename T>
/**冒泡排序*/
class MpSort{
public:
/**
* 交换
*/
void swap(T **array,int a,int b){
T* temp=array[a];
array[a]=array[b];
array[b]=temp;
}
/**
*泛型冒泡
*/
void sort(T **array ,int n,MyComp<T> *comp){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(comp->compare(array[j], array[j+1])>0){
swap(array, j, j+1);
}
}
}
}
};
void printArray(Person **array,int n){
for(Person **p=array;p<array+n;p++){
cout<<(*p)->toString()<<endl;
}
}
int main() {
Person *p1=new Person(1,3);
Person *p2=new Person(2,2);
Person *p3=new Person(3,1);
Person** array=new Person*[3];
array[0]=p1;
array[1]=p2;
array[2]=p3;
MpSort<Person> mpSort;
IdComp *idComp=new IdComp();
//第一次我想根据id排序
mpSort.sort(array,3, idComp);
cout<<"id升序:"<<endl;
printArray(array,3);
//第二次我想根据age排序
cout<<"age升序:"<<endl;
AgeComp *ageComp=new AgeComp();
mpSort.sort(array,3,ageComp);
printArray(array,3);
delete p1;
delete p2;
delete p3;
delete[] array;
return 0;
}
class MyComp{
public:
virtual int compare(T *t1,T *t2)=0;
};
原因在于你不加virtual的话子类的同名的函数不会自动实现多态,跟java不是一个意思.
另外不得不感叹一下java程序员和C++程序员的区别,细微的语法和理念区别直接影响了
程序员的行为,我从学C++那天起就真的从没想过用虚函数做比较器.
还有楼主,你内存泄露了.......
用C++实现的话,这样就很方便:
.............
class Person
{
public:
int id;
int age;
Person(int id, int age).....
string toString().......
};
template<typename T>
/**冒泡排序*/
class MpSort{
public:
void swap(T **array,int a,int b)......
template<typename Fun>
void sort(T **array ,int n,const Fun& fun)
{
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(fun(array[j], array[j+1])>0){
swap(array, j, j+1);
}
}
}
}
};
void printArray(Person **array,int n)...........
int main() {
Person *p1=new Person(1,3);
Person *p2=new Person(2,2);
Person *p3=new Person(3,1);
Person** array=new Person*[3];
array[0]=p1;
array[1]=p2;
array[2]=p3;
MpSort<Person> mpSort;
IdComp *idComp=new IdComp();
//第一次我想根据id排序
mpSort.sort(array,3,[](Person& x,Person& y){ return x.id>y.id; });
cout<<"id升序:"<<endl;
printArray(array,3);
//第二次我想根据age排序
cout<<"age升序:"<<endl;
mpSort.sort(array,3,[](Person& x,Person& y){ return x.age>y.age; });
printArray(array,3);
delete p1;
delete p2;
delete p3;
delete[] array;
return 0;
}
virtual int compare(T *t1,T *t2) = 0;
最好别把 c++ 当 java 用,比如主楼的程序其实不用那么多设计模式的,搞几个函数就行了。
#include <algorithm>
#include <iostream>
#include <sstream>
using namespace std;
struct Person
{
int id;
int age;
friend ostream& operator << (ostream& os, Person const& x)
{
return os << "{age:" << x.age <<",id:" << x.id << "}";
}
};
int ageComp (Person const* const t1, Person const* const t2)
{
return t1->age-t2->age;
}
int idComp (Person const* const t1, Person const* const t2)
{
return t1->id-t2->id;
}
template <typename T, int N, typename C>
void sort (T (&array) [N], C&& comp)
{
for(int i=0;i<N-1;i++)
{
for(int j=0;j<N-1-i;j++)
{
if (comp(array[j],array[j+1])>0)
{
using std::swap;
swap(array[j],array[j+1]);
}
}
}
}
template <typename T, size_t N>
void print_pointer_array (T const (&parray) [N])
{
using std::begin;
using std::end;
std::for_each(begin(parray),end(parray),[](T const& t)
{
cout << *t <<endl;
});
}
int main ()
{
Person const persons [3]
{
{1,3},
{2,2},
{3,1},
};
Person const* pointers [3]
{
&persons[0],
&persons[1],
&persons[2],
};
cout<<"id asc:"<<endl;
sort(pointers,idComp);
print_pointer_array(pointers);
cout<<"age asc:"<<endl;
sort(pointers,ageComp);
print_pointer_array(pointers);
}