如何通过指针在C ++中按字母顺序对命令行参数进行排序

时间:2015-06-01 06:49:50

标签: c++ algorithm sorting pointers

这是代码:

#include <iostream>

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

    // Display each command-line argument.
    for( i = 1; i < argc; i++ )
        cout << "  argv[" << i << "]   " << argv[i] << "\n";
}

如何按指针按字母顺序对输出进行排序?

1 个答案:

答案 0 :(得分:2)

#include <iostream>
#include <algorithm>

using namespace std;

bool mycmp(const string& a, const string &b) {
   return (a<=b);
}


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

    sort(argv+1, argv + argc, mycmp);
    for( i = 1; i < argc; i++ )
        cout << "  argv[" << i << "]   " << argv[i] << "\n";
}