Re: Way to sort in C++, how? 4 cases
Greeting, Andreas,
When you use the "new" operator, the returned result is a pointer to the new object, not a
direct object reference. So, you could do this:
CSortWay * pobj = new CSortWay();
pobj->m_x = 10;
pobj->m_y = 10;
mylistSortWay.push_back(pobj);
Your current code keeps modifying the same object, and pushing pointers to it into the vector.
This won't give you the results you want.
No, casting is not the problem here. You declared your vector to hold pointers to CSortWay
objects, so you must use the correct syntax to dereference pointer members. After using the
iterator to retrieve the pointer from the vector, you must use the arrow ( -> ) operator, not the
dot ( . ) to get to its members.
When calling printf, you probably don't need the carriage return character (\r) before the line
feed (\n).
Also, when declaring and defining your class, it is considered very obsolete to use the typedef
form. To match current practices, I strongly suggest this:
class CSortWay
{
....
};
instead of this:
typedef class CSortWay
{
....
}CSORTWAY;
"Andreas Ott" <aok@dis.microsoft.com> wrote in message news:eRx0Qfk2JHA.3544@TK2MSFTNGP04.phx.gbl...
http://www1.minpic.de/bild_anzeigen.php?id=72043&key=73473177&ende
Hello together,
I have a big problem.
I have 4 cases about the way.
Please see the picture.
Problem:
My code is not working. I don't know why.
Can somebody help me.
C++ Application
Why not work with new?
http://www1.minpic.de/bild_anzeigen.php?id=72120&key=75625063&ende
Must I cast something?
http://www1.minpic.de/bild_anzeigen.php?id=72121&key=83688059&ende
Debug output
http://www1.minpic.de/bild_anzeigen.php?id=72122&key=94859995&ende
With best regards Andreas
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Sort_Wege.cpp : Definiert den Einstiegspunkt f?r die Konsolenanwendung.
//
#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>
typedef class CSortWay
{
public:
CSortWay(){}
~CSortWay(){}
typedef std::vector<CSortWay *> LstResult;
public:
double m_x;
double m_y;
static double SqrtSort(const CSortWay *var1,const CSortWay *var2)
{
register double x1 = var1->m_x;
register double y1 = var1->m_y;
register double x2 = var2->m_x;
register double y2 = var2->m_y;
return( (x1*x1 + y1*y1) < (x2*x2 + y2*y2) );
}
}CSORTWAY;
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
{
//1 10 10
//2 10 20
//3 10 30
//4 10 40
//5 20 40
//6 20 30
//7 20 20
//8 20 10
//9 30 10
//10 30 20
//11 30 30
//12 30 40
//13 40 40
//14 40 30
//15 40 20
//16 40 10
//17 50 10
//18 50 20
//19 50 30
//20 50 40
CSortWay::LstResult mylistSortWay;
CSortWay obj; //= new CSortWay();
obj.m_x = 10;
obj.m_y = 10;
mylistSortWay.push_back(&obj);
obj.m_x = 10;
obj.m_y = 20;
mylistSortWay.push_back(&obj);
obj.m_x = 10;
obj.m_y = 30;
mylistSortWay.push_back(&obj);
obj.m_x = 10;
obj.m_y = 40;
mylistSortWay.push_back(&obj);
obj.m_x = 20;
obj.m_y = 10;
mylistSortWay.push_back(&obj);
obj.m_x = 20;
obj.m_y = 20;
mylistSortWay.push_back(&obj);
obj.m_x = 20;
obj.m_y = 30;
mylistSortWay.push_back(&obj);
obj.m_x = 20;
obj.m_y = 40;
mylistSortWay.push_back(&obj);
obj.m_x = 30;
obj.m_y = 10;
mylistSortWay.push_back(&obj);
obj.m_x = 30;
obj.m_y = 20;
mylistSortWay.push_back(&obj);
obj.m_x = 30;
obj.m_y = 30;
mylistSortWay.push_back(&obj);
obj.m_x = 40;
obj.m_y = 40;
mylistSortWay.push_back(&obj);
obj.m_x = 50;
obj.m_y = 10;
mylistSortWay.push_back(&obj);
obj.m_x = 50;
obj.m_y = 20;
mylistSortWay.push_back(&obj);
obj.m_x = 50;
obj.m_y = 30;
mylistSortWay.push_back(&obj);
obj.m_x = 50;
obj.m_y = 40;
mylistSortWay.push_back(&obj);
std::sort(mylistSortWay.begin(),
mylistSortWay.end(),
CSortWay::SqrtSort);
for ( register CSortWay::LstResult::iterator it2(mylistSortWay.begin());
it2!=mylistSortWay.end();
it2++ )
{
/* printf("Pos=%d x= %7.2f, y=%7.2f\r\n",
it2,
(*it2).m_x,
(*it2).m_y);*/
}
}
return nRetCode;
}