Solved

Sorting CList, CArray etc: how ?

Posted on 2004-03-27
11
897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Turn a spreadsheet into a vba executable. 2 103
countEvens challenge 2 117
matchUp  challenge 9 134
How do I get Window Title of all opened process? 4 61
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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

734 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