custom sort for std::set and CArray
// set_test.cpp : Test customized sort function for a set
#include "stdafx.h"
#pragma warning(disable:4786)
#include <set>
#include <iostream>
class bb
{
public:
bb(int i)
{
m_i = i;
}
~bb()
{
}
bool operator<(const bb& abb) const
{
return m_i<abb.m_i;
}
void seti(int i)
{
m_i = i;
}
int geti()
{
return m_i;
}
private:
int m_i;
};
struct compare_bb
{
//welcome to www.fruitfruit.com
// VC6(SP6)
bool operator()(const bb* a, const bb* b)
{
if(!a)
return false;
if(!b)
return true;
return a->operator<(*b);
}
};
int main(int argc, char* argv[])
{
typedef std::set<bb*,compare_bb> BBSet;
BBSet bcol;
bcol.insert(new bb(3));
bcol.insert(new bb(1));
bcol.insert(new bb(2));
for(BBSet::iterator it = bcol.begin();
it!= bcol.end();
it++)
std::cout<<(*it)->geti()<<std::endl;
return 0;
}
// sort_carray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sort_carray.h"
#include <afxtempl.h>
#include <string>
#include <sstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
struct MyData
{
MyData():m_data1(0),m_data2(-1){
}
MyData(int a, double b):m_data1(a),m_data2(b){
}
int m_data1;
double m_data2;
std::string to_string()
{
std::stringstream ss;
ss<<"{"<<m_data1<<","<<m_data2<<"}"<<std::endl;
return ss.str();
}
};
int bydouble(const void * v1, const void * v2)
{
MyData data1 = *(MyData *)v1;
MyData data2 = *(MyData *)v2;
return data1.m_data2 - data2.m_data2;
} // byname1
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CArray<MyData,MyData> myarray;
myarray.Add(MyData(1,1.2));
myarray.Add(MyData(2,0.2));
myarray.Add(MyData(2,5.2));
for(int i=0;i<myarray.GetSize();i++)
{
MyData tmp = myarray.GetAt(i);
std::cout<<tmp.to_string()<<std::endl;
}
qsort(myarray.GetData(),myarray.GetSize(),sizeof(MyData),bydouble);
std::cout<<"after sort"<<std::endl;
for( i=0;i<myarray.GetSize();i++)
{
MyData tmp = myarray.GetAt(i);
std::cout<<tmp.to_string()<<std::endl;
}
}
return nRetCode;
}
#include "stdafx.h"
#pragma warning(disable:4786)
#include <set>
#include <iostream>
class bb
{
public:
bb(int i)
{
m_i = i;
}
~bb()
{
}
bool operator<(const bb& abb) const
{
return m_i<abb.m_i;
}
void seti(int i)
{
m_i = i;
}
int geti()
{
return m_i;
}
private:
int m_i;
};
struct compare_bb
{
//welcome to www.fruitfruit.com
// VC6(SP6)
bool operator()(const bb* a, const bb* b)
{
if(!a)
return false;
if(!b)
return true;
return a->operator<(*b);
}
};
int main(int argc, char* argv[])
{
typedef std::set<bb*,compare_bb> BBSet;
BBSet bcol;
bcol.insert(new bb(3));
bcol.insert(new bb(1));
bcol.insert(new bb(2));
for(BBSet::iterator it = bcol.begin();
it!= bcol.end();
it++)
std::cout<<(*it)->geti()<<std::endl;
return 0;
}
// sort_carray.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sort_carray.h"
#include <afxtempl.h>
#include <string>
#include <sstream>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// The one and only application object
CWinApp theApp;
using namespace std;
struct MyData
{
MyData():m_data1(0),m_data2(-1){
}
MyData(int a, double b):m_data1(a),m_data2(b){
}
int m_data1;
double m_data2;
std::string to_string()
{
std::stringstream ss;
ss<<"{"<<m_data1<<","<<m_data2<<"}"<<std::endl;
return ss.str();
}
};
int bydouble(const void * v1, const void * v2)
{
MyData data1 = *(MyData *)v1;
MyData data2 = *(MyData *)v2;
return data1.m_data2 - data2.m_data2;
} // byname1
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
cerr << _T("Fatal Error: MFC initialization failed") << endl;
nRetCode = 1;
}
else
{
CArray<MyData,MyData> myarray;
myarray.Add(MyData(1,1.2));
myarray.Add(MyData(2,0.2));
myarray.Add(MyData(2,5.2));
for(int i=0;i<myarray.GetSize();i++)
{
MyData tmp = myarray.GetAt(i);
std::cout<<tmp.to_string()<<std::endl;
}
qsort(myarray.GetData(),myarray.GetSize(),sizeof(MyData),bydouble);
std::cout<<"after sort"<<std::endl;
for( i=0;i<myarray.GetSize();i++)
{
MyData tmp = myarray.GetAt(i);
std::cout<<tmp.to_string()<<std::endl;
}
}
return nRetCode;
}