-
August 28th, 2003, 01:12 AM
#1
Need help with sort()
Can i know how to use the <algorithm> sort function in c++?
Is it possible to use that to sort the order in my string array?
Thanks
-
August 28th, 2003, 02:06 AM
#2
You can use qsort to sort your string array
Code:
int sortfunc(const void *elem1, const void *elem2)
{
return strcmp(*((LPSTR*)elem1), *((LPSTR*)elem2));
}
...
LPSTR strs[] = {"String4", "String1", "String3", "String2"};
qsort(&strs[0], 4, sizeof(strs[0]), sortfunc);
Hope it will help you
-
August 28th, 2003, 02:23 AM
#3
-
August 28th, 2003, 02:25 AM
#4
Originally posted by rxbagain
You can use qsort to sort your string array
Well...one side note....you should not use 'qsort()' with STL types like 'vector' for several reasons like for example:
- 'qsort()' is not guaranteed to work with non-POD types (structures and classes)
- 'qsort()' is not type-safe
-
August 28th, 2003, 03:54 AM
#5
hmm how can i use the code to sort 2D array?
i need a more simple code on the sort()...
thanks
-
August 28th, 2003, 03:58 AM
#6
Well....how does your array look like?
-
August 28th, 2003, 04:03 AM
#7
i have a storage[10][100] array....
storing 10 string.
i wanted to sort it by name.
how can i use the code to modify to this?
-
August 28th, 2003, 04:16 AM
#8
Code:
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <conio.h>
int main()
{
std::vector<std::string> StringArray;
StringArray.push_back("String One");
StringArray.push_back("String Four");
StringArray.push_back("String Two");
StringArray.push_back("String Three");
std::sort(StringArray.begin(), StringArray.end());
// Print results
for(std::vector<std::string>::iterator iter = StringArray.begin();
iter != StringArray.end();
++iter)
std::cout << *iter << std::endl;
// Wait for keystroke
_getch();
return 0;
}
-
August 28th, 2003, 04:51 AM
#9
a) You should use std::vector<std::string>. Much easier to handle instead of char[10][100].
Code:
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
typedef std::vector<std::string> StringArray;
using namespace std;
StringArray S(3, "");
int main()
{
S[0] = "Paul";
S[1] = "John";
S[2] = "Alan";
cout << "Before sort:" << endl;
cout << S[0] << " " << S[1] << " " << S[2] << endl;
std::sort(S.begin(), S.end() );
cout << "After sort:" << endl;
cout << S[0] << " " << S[1] << " " << S[2] << endl;
}
The problem with sorting a char[10][100] is that the element that you are sorting (a char[100]) is not an l-value, and std::sort() requires that what you're sorting is an l-value. A wrapper for the array is required:
Code:
#include <algorithm>
#include <iostream>
using namespace std;
struct Wrapper
{
Wrapper(const char *val="") { strcpy(Char100, val); }
char Char100[100];
};
bool SortIt( const Wrapper& first, const Wrapper& second)
{
int result = strcmp(first.Char100, second.Char100);
return (result < 0)?true:false;
}
int main()
{
Wrapper GlobArray[] = {"Paul", "John", "Alan"};
cout << "Before sort:" << endl;
cout << GlobArray[0].Char100 << " " << GlobArray[1].Char100 << " " << GlobArray[2].Char100 << endl;
cout << "After sort" << endl;
std::sort(GlobArray, GlobArray + 3, SortIt);
cout << GlobArray[0].Char100 << " " << GlobArray[1].Char100 << " " << GlobArray[2].Char100 << endl;
}
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 28th, 2003 at 05:41 AM.
-
August 28th, 2003, 05:06 AM
#10
Originally posted by Andreas Masur
Well...one side note....you should not use 'qsort()' with STL types like 'vector' for several reasons like for example:
- 'qsort()' is not guaranteed to work with non-POD types (structures and classes)
- 'qsort()' is not type-safe
Not only that, the compiler can optimize std::sort and the functor more aggressively than qsort(). In truth, there is no reason whatsoever to use qsort() in a C++ program unless the compiler's library doesn't have STL (which is rare).
Even if the compiler doesn't have STL, you still end up having to write your own sort, since as you pointed out, qsort() is not guaranteed to work for non-POD types.
Regards,
Paul McKenzie
-
August 28th, 2003, 06:02 AM
#11
StringArray S(3, "");
this is equivilant to S[3][??] ??
how many characters does it takes in?
sorry quite new to STL stuff
Paul: i get 4 warning with your code.....
warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
And 1 more serious problem... fstream f.open cannot open the array....
error C2664: 'fopen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
wad is that? anyways to solve?
Thanks
Last edited by ayumi; August 28th, 2003 at 06:34 AM.
-
August 28th, 2003, 08:26 AM
#12
Originally posted by ayumi
StringArray S(3, "");
this is equivilant to S[3][??] ??
how many characters does it takes in?
A vector is a dynamic array. The vector type is std::string. The vector has 3 std::strings, each initialized to "". A std::string grows dynamically, so the number of characters that can be stored is theoretically unlimited.
Paul: i get 4 warning with your code.....
Those warnings only mean that the debugger will recognize only the first 255 characters. They are harmless and can be turned off with
#pragma warning (disable : 4786)
And 1 more serious problem... fstream f.open cannot open the array....
error C2664: 'fopen' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'const char *'
std::string whatever;
fopen( whatever.c_str(), "r");
The c_str() member function returns a const char *.
sorry quite new to STL stuff
Get "Accelerated C++" by Koenig & Moo. You would have learned immediately not to use char[10][100] and instead do what I posted.
Regards,
Paul McKenzie
-
August 28th, 2003, 09:15 AM
#13
Originally posted by ayumi
warning C4786: 'std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >
>::~vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >' : identifier was truncated to '255' characters in the debug information
Take a look at this FAQ...
-
August 28th, 2003, 09:17 AM
#14
Since you are new to the 'vector' class, besides the book Paul already mentioned...take a look at this introduction to the vector class...
-
August 28th, 2003, 09:33 AM
#15
wow... thanks a lot paul and andreas and those who helped me!!
another thing is ..... do i need to delete the vector after used??
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|