For Programmers: Free Programming Magazines  


Home > Archive > VC Language > November 2005 > The Qsort









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author The Qsort
NewVisualUser

2005-11-22, 7:04 pm

I have the following:
struct Books
{
string bookname;
int bookid;
};
....
Books Library[500];

Library is then filled by user, and ofcourse in not done in alphabetic
order. So, what I want to do after inserting values into Library is
sort it alphabetically by Library.bookname.
How can I use the Visual C++ qsort built in to accomplish that.
I have read the help file in visual for qsort but to tell you the truth
I couldnt make much of it. I know that before calling the qsort, I
have to first write a compare function. But really, more than that I
dont know.

Please Help
Thanks in Advance

Frank Hickman [MVP]

2005-11-22, 9:59 pm

"NewVisualUser" <sakitah2000@yahoo.com> wrote in message
news:1132706951.180247.109070@g44g2000cwa.googlegroups.com...
>I have the following:
> struct Books
> {
> string bookname;
> int bookid;
> };
> ...
> Books Library[500];
>
> Library is then filled by user, and ofcourse in not done in alphabetic
> order. So, what I want to do after inserting values into Library is
> sort it alphabetically by Library.bookname.
> How can I use the Visual C++ qsort built in to accomplish that.
> I have read the help file in visual for qsort but to tell you the truth
> I couldnt make much of it. I know that before calling the qsort, I
> have to first write a compare function. But really, more than that I
> dont know.
>
> Please Help
> Thanks in Advance
>


Off the top of my head, try this...

int CompareBooks( const void* pElement1, const void* pElement2 )
{
struct Books* pLVal= static_cast<struct Books*>( *pElement1 );
struct Books* pRVal= static_cast<struct Books*>( *pElement1 );

return pLVal->bookname.compare( pRVal->bookname );
}

....
qsort( static_cast<void*>( Library ), nElementCnt, sizeof( struct Books ),
CompareBooks );


You may need to tweak the compare function...

--
============
Frank Hickman
Microsoft MVP
NobleSoft, Inc.
============
Replace the _nosp@m_ with @ to reply.


Carl Daniel [VC++ MVP]

2005-11-23, 3:59 am

NewVisualUser wrote:
> I have the following:
> struct Books
> {
> string bookname;
> int bookid;
> };
> ...
> Books Library[500];
>
> Library is then filled by user, and ofcourse in not done in alphabetic
> order. So, what I want to do after inserting values into Library is
> sort it alphabetically by Library.bookname.
> How can I use the Visual C++ qsort built in to accomplish that.
> I have read the help file in visual for qsort but to tell you the
> truth I couldnt make much of it. I know that before calling the
> qsort, I have to first write a compare function. But really, more
> than that I dont know.


Don't use qsort - that's the old C runtime library function that's not
typesafe.

Instead, use std::sort, which is typesafe, easier to use, and generally
faster.

<untested-code>

#include <algorithm>
#include <string>

using namespace std;

struct Book
{
string bookname;
int bookid;
};

Book Library[500];

inline bool operator < (const Book& khs, const Book& rhs)
{
return lhs.bookname < rhs.bookname;
}

// ...
sort(Library,Library+500); // or however many are valid

</untested-code>

-cd





peter.koch.larsen@gmail.com

2005-11-23, 7:04 pm


NewVisualUser skrev:

> I have the following:
> struct Books
> {
> string bookname;
> int bookid;
> };
> ...
> Books Library[500];


500 books? Thats an awfully rigid number! Probably better to use
std::vector.
>
> Library is then filled by user, and ofcourse in not done in alphabetic
> order. So, what I want to do after inserting values into Library is
> sort it alphabetically by Library.bookname.
> How can I use the Visual C++ qsort built in to accomplish that.

You can not use qsort to sort non-pod data. qsort moves elements around
by using memcpy and this causes undefined behaviour with your struct
(which - because of the bookname member - is not pod). Use std::sort
instead.
> I have read the help file in visual for qsort but to tell you the truth
> I couldnt make much of it. I know that before calling the qsort, I
> have to first write a compare function. But really, more than that I
> dont know.


You will also have to write a compare function with std::sort, but
apart from this std::sort is much simpler. Look it up and come back if
you still have any questions.
>
> Please Help
> Thanks in Advance


/Peter

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com