these member-vars represent the attributes filled by the content of a directory, and the flag bIsDir shows if it is an directory or not.
the results are saved in an vector<file_struct>vc; for example:
so i've sorted my array with directories and other files...
but now i'm searching for an possibility to sort the directory-elements and the file-elements in an alphabetical order which should lokk like similar to this way:
qsort is a C function: it doesn't know anything about vectors. If you intend to use qsort, you have to give it an array of pointers to structures: not a vector.
If you intend to use vectors, use Philip Nicoletti's solution.
jfaust
September 10th, 2002, 01:42 PM
I believe qsort will work on vector containers, because the storage is guaranteed to be contiguous. It won't work, however, on other STL containers.
Still, use std::sort.
Jeff
Graham
September 11th, 2002, 06:02 AM
But the OP's file_struct is not POD, so qsort won't............ oh, no, not again! :mad:
Paul McKenzie
September 11th, 2002, 06:11 AM
Originally posted by jfaust
I believe qsort will work on vector containers, because the storage is guaranteed to be contiguous. It won't work, however, on other STL containers.
Still, use std::sort.
Jeff Hello Jeff,
qsort() is only guaranteed to work on POD types. The OP has std::string, making the class non-POD. Therefore the only safe method is std::sort (remember the Portland compiler? ;))
Regards,
Paul McKenzie
jfaust
September 11th, 2002, 10:10 AM
Boy, do I remember...
The point I was attempting to make is correct. Unfortunately, the example wasn't.
Jeff
Steve Baerthlein
September 11th, 2002, 12:56 PM
thank you all for your explenations!!!!
is it possible to sort a std::vector with std::sort this way?
my vector filled with std:string elements:
1 c
2 d
3 a
4 e
5 b
now i want to sort the first 3 elements like this:
1 a
2 c
3 d
4 e
5 b
and afterwards i sort the last 2 elements, so that the content of my vector looks like:
1 a
2 c
3 d
4 b
5 e
is it possible to change the position of the iterator to do this (just a scetch, symbolic syntax...):
inline bool c(const string &lhs, const string &rhs)
{
return lhs < rhs;
}
but i'm not able to compile this code due to:
error C2678: binary '<' : no operator defined which takes a left-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' (or there is no
acceptable conversion)
(i'm using vc++6.0)
what i would like to use is:
int CompName(const void* lhs, const void* rhs)
{
return _stricmp(((file_struct*)lhs)->strName.c_str(), ((file_struct*)rhs)->strName.c_str());
}
and calling afterwards
qsort(static_cast<void*>(&vc[0]), vc.size(), sizeof(file_struct), CompDir);
but i'm only able to sort the whole array and not parts of it with qsort which is my desire....
perhaps i have to write my own sort - function. i can't use the default stl sort-function because of the operator<(a,b) which would be rather complicate to implement for my structur, and also a bool compare-function would be (how should equal elements be sorted aso...)
thank you for your patience,
bye,
stefan
Paul McKenzie
September 11th, 2002, 05:08 PM
Hello Steve,
Your comparison is still wrong. Since you have a vector of file_struct, your comparison takes references to file_struct, not string. Here is a stripped down example. Adapt it to your example.
#include <string>
#include <vector>
#include <algorithm>
class A
{
public:
std::string sa;
};
int main()
{
std::sort(VA.begin(), VA.end(), c);
}
Note that this compiles cleanly. You are passing to std::sort's comparison function A objects, not std::string's. And as I pointed out, your class is a non-POD (plain old data) class because of std::string. Therefore you should not use qsort() on it since sorting non-POD objects is not guaranteed to work with qsort().
Regards,
Paul McKenzie
Philip Nicoletti
September 11th, 2002, 06:04 PM
Sorry. From your one message I thought the vector was
a vector<string> , not vector<file_struct>.
As Paul mentioned, if you are sorting vector<A> , the
comparison fucntion prototype will be :
bool comp(const A& lhs, const A& rhs);
Is there a reason you abandoned your first thought of
just using one sort and having the comparison put
the directories first ?
Here is a simple working example. I also made the comparison
function a functor - it is a little faster that way. Notice
the "()" after the sortCompare in the call to sort, and notice
the way sortCompare is written. It can also be made faster by
using specialization, but unless you have a large number of
elements it will not make that much difference.
just wanted to say thank you very muchtoo all of you.
that was exactly what i was searching for. i'm sorry for my confusing postings, i just wanted to achieve those things i wrote in my first posting, later on i tried only to cope with the more simple string-vector-thing, but finally you told me how to succseed....
thanx,
stefan
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.