Solved

Sorting CList, CArray etc: how ?

Posted on 2004-03-27
11
879 Views
Last Modified: 2013-11-20
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.

0
Comment
Question by:mrwad99
  • 5
  • 5
11 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10695112
Check out http://www.codeproject.com/cpp/colsort.asp ("General Purpose Collection Sorter")
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10695190
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 !
0
 
LVL 86

Expert Comment

by:jkr
ID: 10695215
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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:GJanusz
ID: 10695548
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.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10695562
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.
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 10695590
>>Are there any situtations from your personal experience where you cannot mix STL with
>>MFC ?

"Cannot" is relative. It is all a matter of the effort you want to put in. The always easiest solution is to separate the stuff into different source files or modules with clean interfaces, then you are most unlikely to experience trouble.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10695593
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 ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 10695604
Yes, that's what I meant.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10695668
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
0
 
LVL 86

Expert Comment

by:jkr
ID: 10695684
>>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.
0
 
LVL 19

Author Comment

by:mrwad99
ID: 10695874
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 !
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 69
Best tools for Backup and restore of Wordpress site 6 57
viewing source code from eclipse 13 110
lucky13 challenge 11 154
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question