注册 登录
编程论坛 C++教室

[讨论][求助]syntax to use stl sort() on char arrays

HJin 发布于 2007-09-22 00:52, 679 次点击

#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;

struct cmp
{
//Return true if s1 < s2; otherwise, return false.
bool operator()(const char *s1, const char * s2)
{
return strcmp(s1, s2) < 0;
}
};


int main(int argc, char* argv[])
{
int i;

sort(argv, argv + argc, cmp());
for (i = 0; i < argc; ++i)
{
cout << argv[i] << endl;
}

char s[][6] = {"sun", "moon", "earth"};

/**
need help here: sort char arrays. I cannot get the syntax working.

Please suggest a way to call sort() as simple as possible, including writing a new
predictor for sort().
*/
// sort(s, s + 3);

for (i = 0; i < 3; ++i)
{
cout << s[i] << endl;
}


return 0;
}

[此贴子已经被作者于2007-9-22 1:56:00编辑过]

0 回复
1