|
It can be posible to (bubble) sort an ObList after some criteria ?
Here I have :
typedef CTypedPtrList<<CObList, CDrawObj*>> CDrawObjList;
and then :
CDrawObjList ObjList;
for(int i = 0;i < 5;++i)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
ObjList.AddTail(pObj)
}
ok , by now seems to be 5 element in objects array
My question is , how can sort elements of ObjList after follow criteria :
POSITION pos = ObjList.GetHeadPosition();
while(pos)
{
CDrawObj* pObj1 = ObjList.GetNext(pos);
if(pos == NULL)break;
CDrawObj* pObj2 = ObjList.GetNext(pos);
if(pObj1->m_position.left > pObj2->m_position.left)
}
any hint or ideas will be apreciated . Thank you !!!
modified on Tuesday, December 14, 2010 7:38 AM
|
|
|
|
|
Here is a useful article on CodeProject to get you started: Using the std::sort() Method[^].
Just say 'NO' to evaluated arguments for diadic functions! Ash
|
|
|
|
|
The article sorts a STL collection class, but this just complement my answer
|
|
|
|
|
Do you really need to use the MFC collection classes ? If you can use something else, I strongly suggest you start using the STL collections (in your case, the std::list). There are algorithms that will let you sort a std::list easily (a quick google search would bring you what you need).
|
|
|
|
|
Well , I must change much code to use std::sort ( plus , I never used before ) , it is the only way ? I mean , how can I switch position between two elements of an objects array ?
|
|
|
|
|
mesajflaviu wrote: plus , I never used before
This is just a bad excuse . The STL collection classes are much more powerful than the MFC collection classes, I strongly suggest you start using them instead of the MFC collections, you won't regret it (it takes a bit of time to get started with it but once you know how to use them, you will gain a lot of time).
mesajflaviu wrote: I mean , how can I switch position between two elements of an objects array ?
You can still remove an element and insert it back at the correct location but this is ugly and prone to errors. I'm not aware of another alternative (but I didn't use the MFC collections for long).
|
|
|
|
|
I think that I try in both way .. thanks !
|
|
|
|
|
Well, you could use the stl algorithms work with the MFC collection classes too. That's the unprecedented beauty of STL.
It was ever thus, the Neophiles will always rush out and get 'The Latest Thing' at a high price and with all the inherent faults - Dalek Dave.
|
|
|
|
|
If I mix MFC with STL , I wouldn't keep consistency , or not ?
|
|
|
|
|
It's been a while (and I don't have my book with me) but iirc, the std::algorithm (and I think sort is in that) didn't require the structure to be a std::list. (I seem to recall standard arrays being able to be sorted with std as well.)
|
|
|
|
|
// switch pObj1 and pObj2 between them ... but how ?
Try it :
void SortDrawObjects(CDrawObjList& cList)
{
for (int i = 0; i < cList.GetCount(); i++) {
POSITION pos = cList.GetHeadPosition();
while (pos) {
POSITION posFirst = pos;
CDrawObj* pcFirst = cList.GetNext(pos);
if (pos) {
POSITION posSecond = pos;
CDrawObj* pcSecond = cList.GetNext(pos);
if (pcFirst->m_position.left > pcSecond->m_position.left) {
cList.SetAt(posFirst, pcSecond);
cList.SetAt(posSecond, pcFirst);
}
pos = posSecond;
}
}
}
}
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
Well , it's work great ! Thank you very much !
|
|
|
|
|
An algorithm after all is just a modus operandi. As long as a collection class, irrespective of the library that is part of, provides mechanism to insert and remove elements at any location, all the algorithms can be implemented. The performance may be not as expected due to the implementation of the classes themselves.
What I am trying to say is, if you understand the algorithm and read the documentation for the collection class, it must not be difficult at all.
|
|
|
|
|
|
|
If you are wedded to MFC, at least use a CObArray instead of a CObList. The algorithim will be much cleaner and faster with a smaller memory footprint.
For instance ...
typedef CTypedPtrArray<<CObArray, CDrawObj*>> CDrawObjArray;
then create the array ...
CDrawObjArray ObjArray;
const unsigned int nSize = 5;
ObjArray.SetSize(nSize);
fill the array ...
for(int i = 0; i < nSize; i++)
{
CDrawObj* pObj = new CDrawObj(CRect(ptOrigin,sizeRect));
ObjArray[i] = pObj;
}
and finally sort the array (using the most inefficient sorting algorithim known to man).
for(int i = 0; i < nSize; i++)
{
for(int j = i; j < nSize; j++)
{
unsigned int nPrevious = ObjArray[j-1]->m_position.left;
unsigned int nCurrent = ObjArray[j]->m_position.left;
if(nPrevious > nCurrent)
{
CDrawObj* pObj = ObjArray[j-1];
ObjArray[j-1] = ObjArray[j];
ObjArray[j] = pObj ;
}
}
}
modified 17-Jan-19 15:57pm.
|
|
|
|
|
Michael Waters wrote: ...(using the most inefficient sorting algorithim known to man).
I can think of one worse.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Guys , you're overcome me with solutions !!! I want to kindly thank you ! I was learn something here .And after all , seems to must begin use STL ... I will googling and see what I find ... You are very kind !
|
|
|
|
|
hi,
I don't know Graphics in vc++? i want how to develop line graph in vc++?
Win32 GUI use line graph is possible?
any sample code or url please give me
Regards,
M.Mathivanan
|
|
|
|
|
I don't understand from your query on which base do you want to draw the line on.
If it's a Win32 SDI application, you can get the device context and then draw the lines on the graph using the MoveTo and LineTo API's.
I am a HUMAN. I have that keyword (??? too much) in my name........
_AnsHUMAN_b>
|
|
|
|
|
|
Are you using MFC or pure win32 API ? If you are using MFC, take a look at my sig for a charting control I wrote for codeproject.
|
|
|
|
|
|
pDC->MoveTo(x,y);
while(oPoint in points collection)
{
pDC->LineTo(oPoint.x,oPoint.y);
}
onwards and upwards...
|
|
|
|
|
ms chart active x control can be alternative
|
|
|
|