Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting CList, CArray etc: how ?

Posted on 2004-03-27
11
Medium Priority
?
929 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Loops Section Overview
Suggested Courses

916 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