Solved

Sorting CList, CArray etc: how ?

Posted on 2004-03-27
11
849 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 21
Question regarding Copy/Paste 16 86
format the code in java 6 72
scoreUp challenge 14 48
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now