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
Printable View
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
You can use qsort to sort your string arrayHope it will help youCode: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);
Well...one side note....you should not use 'qsort()' with STL types like 'vector' for several reasons like for example:Quote:
Originally posted by rxbagain
You can use qsort to sort your string array
- 'qsort()' is not guaranteed to work with non-POD types (structures and classes)
- 'qsort()' is not type-safe
hmm how can i use the code to sort 2D array?
i need a more simple code on the sort()...
thanks
Well....how does your array look like?
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?
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;
}
a) You should use std::vector<std::string>. Much easier to handle instead of char[10][100].
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 <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;
}
Regards,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;
}
Paul McKenzie
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).Quote:
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
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
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
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.Quote:
Originally posted by ayumi
StringArray S(3, "");
this is equivilant to S[3][??] ??
how many characters does it takes in?
Those warnings only mean that the debugger will recognize only the first 255 characters. They are harmless and can be turned off withQuote:
Paul: i get 4 warning with your code.....
#pragma warning (disable : 4786)
std::string whatever;Quote:
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 *'
fopen( whatever.c_str(), "r");
The c_str() member function returns a const char *.
Get "Accelerated C++" by Koenig & Moo. You would have learned immediately not to use char[10][100] and instead do what I posted.Quote:
sorry quite new to STL stuff
Regards,
Paul McKenzie
Take a look at this FAQ...Quote:
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
Since you are new to the 'vector' class, besides the book Paul already mentioned...take a look at this introduction to the vector class...
wow... thanks a lot paul and andreas and those who helped me!!
another thing is ..... do i need to delete the vector after used??