Re: Way to sort in C++, how? 4 cases
Hello Joseph,
thank for your help.
You are right.
I delete it register
I change from pointer to reference
I don't use m_ variables
I use static. Only with static I can call this function.
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
It is good or bad?
Maybe better with a function of the object, or?
Problem
It is also not working. It is not correct.
Pos=1 x= 10.00, y= 10.00
Pos=2 x= 10.00, y= 20.00
Pos=3 x= 10.00, y= 30.00
Pos=4 x= 30.00, y= 10.00
Pos=5 x= 30.00, y= 20.00
Pos=6 x= 10.00, y= 40.00
Pos=7 x= 30.00, y= 30.00
Pos=8 x= 30.00, y= 40.00
Pos=9 x= 50.00, y= 10.00
Pos=10 x= 50.00, y= 20.00
Pos=11 x= 50.00, y= 30.00
Pos=12 x= 50.00, y= 40.00
Pos=13 x= 70.00, y= 10.00
Pos=14 x= 70.00, y= 20.00
Pos=15 x= 70.00, y= 30.00
Pos=16 x= 70.00, y= 40.00
Pos=17 x= 90.00, y= 10.00
Pos=18 x= 90.00, y= 20.00
Pos=19 x= 90.00, y= 30.00
Pos x= 90.00, y= 40.00
I need the algorithm for all 4 cases.
Can you give me that?
Can you say, where can I reread it?, if you not want to give me the code?
My code now - Thnaks for check!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~+
// Sort_Wege.cpp :
//
#include "stdafx.h"
#include "Sort_Wege.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// Das einzige Anwendungsobjekt
CWinApp theApp;
using namespace std;
#pragma once
#include <vector>
#include <algorithm>
#include <conio.h>
#include <ctype.h>
class CSortWay
{
public:
CSortWay(){}
CSortWay(double x, double y) { X = x; Y = y; }
~CSortWay(){}
typedef std::vector<CSortWay> data;
public:
double X;
double Y;
static double SortCase1(const CSortWay var1,const CSortWay var2)
{
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase2(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase3(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
static double SortCase4(const CSortWay var1,const CSortWay var2)
{
// ** TODO
double x1 = var1.X;
double y1 = var1.Y;
double x2 = var2.X;
double y2 = var2.Y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
};
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// MFC initialisieren und drucken. Bei Fehlschlag Fehlermeldung aufrufen.
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: Den Fehlercode an Ihre Anforderungen anpassen.
_tprintf(_T("Schwerwiegender Fehler bei der MFC-Initialisierung\n"));
nRetCode = 1;
}
else
{
CSortWay::data mylistSortWay;
mylistSortWay.push_back(CSortWay(10,10));
mylistSortWay.push_back(CSortWay(10,20));
mylistSortWay.push_back(CSortWay(10,30));
mylistSortWay.push_back(CSortWay(10,40));
mylistSortWay.push_back(CSortWay(30,10));
mylistSortWay.push_back(CSortWay(30,20));
mylistSortWay.push_back(CSortWay(30,30));
mylistSortWay.push_back(CSortWay(30,40));
mylistSortWay.push_back(CSortWay(50,10));
mylistSortWay.push_back(CSortWay(50,20));
mylistSortWay.push_back(CSortWay(50,30));
mylistSortWay.push_back(CSortWay(50,40));
mylistSortWay.push_back(CSortWay(70,10));
mylistSortWay.push_back(CSortWay(70,20));
mylistSortWay.push_back(CSortWay(70,30));
mylistSortWay.push_back(CSortWay(70,40));
mylistSortWay.push_back(CSortWay(90,10));
mylistSortWay.push_back(CSortWay(90,20));
mylistSortWay.push_back(CSortWay(90,30));
mylistSortWay.push_back(CSortWay(90,40));
// ** Case 1
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase1);
int count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 2
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase2);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 3
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase3);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
// ** Case 4
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SortCase4);
count=0;
for ( CSortWay::data::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
++count;
printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
count,
(*it2).X,
(*it2).Y);
}
getch();
}
return nRetCode;
}