Link to home
Start Free TrialLog in
Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Sorting CList, CArray etc: how ?

Hello.  

I was wondering, is there an easy way to sort a CList, CArray etc ?  I initially figured that there would be some sort (hehe) of sort() function like there is for STL vector, but there is not.  In fact there is nothing even resembling this.  I really don't want to have to write my own sorting algorithm as this feels a bit like re-inventing the wheel.

I thought about possibly cheating a bit, by creating a STL vector, copying the CList elements into it, sorting the vector in the normal STL way and then copying the elements back to the CList.  As good as this sounds I have no idea if it would be excepted to mix STL with MFC; indeed if it is possible or not.

Do we mix STL components with MFC or not, and if not how can I sort my CList ??

Thanks in advance.

Avatar of jkr
jkr
Flag of Germany image

Check out http://www.codeproject.com/cpp/colsort.asp ("General Purpose Collection Sorter")
Avatar of mrwad99

ASKER

Right ok, that link looks good and will require more reading.  

In the meantime however, can you tell me in general whether or not it is a bad idea to use STL components with MFC ?  I tried using a Vector, thus had to add #include <vector> and 'using namespace std' to my code, which added a monsterous amount of apparent 'errors'.  Why was this; is the STL not compatible with MFC or something ?

Thanks !
Mixing MFC and STL can be a major headache. See http://support.microsoft.com/default.aspx?scid=kb;en-us;143207 ("PRB: Common Problems When Using STL with STD Namespace") and the ALLINONE sample at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcsample/html/_sample_com_allinone.asp
Avatar of Member_2_2667525
Member_2_2667525

I've used MFC and STL together extensively (as many here have, I'm sure), and have not had any problems.  I use #pragma to turn off the long name warning.  The bigger issue I have is simply understanding STL template code sufficiently so I can understand what the problem is when I need to step into it.  It's not a problem with STL being used with MFC, but rather, my unmastered understanding of templates and STL's rather shorthand variable names and lack of in-code documentation.
Avatar of mrwad99

ASKER

Ok then GJanusz, why do I get loads of errors when I try to add a vector a standard MDI app ?

Also jkr; the first link you gave detailing problems with the STL stated that it only applies to VC++ 4.0; I have VC++ 6.0.  Are there any situtations from your personal experience where you cannot mix STL with MFC ?

Thanks again.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrwad99

ASKER

Sorry, not clear on that last one jkr...

Separating stuff; ok - does that mean that for example you could have an MFC app that used class X, and class X made extensive use of STL to the point where *all STL code was contained within the source code for class X* ?  Would that be ok ?
Yes, that's what I meant.
Avatar of mrwad99

ASKER

Right, ok.  I realised what I was doing wrong, I was letting VC++ help me too much.

When I typed "vector<", VC++ popped up its little tooltip presenting a vector type that took two parameters, i.e.

vector<typename _ty, typename _A=Allocator<_Ty>>

so I was trying out

vector<int, int>

instead of

vector<int>

hence getting loads of errors.  Again, this comes from CList wanting two parameters...

Before I close this question, can someone tell me what *this* (i.e. the one above) type of vector is, as I have never seen it before ?

TIA
>>vector<typename _ty, typename _A=Allocator<_Ty>>

This is the standard declaration for a std::vector - the 2nd template argument is an allocator that defaults to 'Allocator<type>' like a C++ default argument, e.g. 'Allocator<int>' for integers.
Avatar of mrwad99

ASKER

Right, answer accepted as it has shown that MFC can be used with STL, and an alternative method was offered in the form of the link given.

Thanks a lot !