如何使用Alphanum算法在C ++中对文本行进行排序?

时间:2017-08-27 14:55:34

标签: c++ algorithm

我想使用Alphanum算法对* .txt文件中包含的一些文本行进行排序。

我见过这个网站: http://www.davekoelle.com/alphanum.html

并且有一个包含此算法的* .cpp文件。

但我不知道如何使用它。

所以让我说我有这样的代码:

#include <iostream>
#include <fstream>

using namespace std;


int main()
{

    ifstream file;
    file.open("My_textfile.txt");

    [ALPHANUM SORTS My_textfile.txt --> don't know how to do it :( ]

    file.close();
    return 0;

}

有没有简单的方法可以使用这种算法进行排序?

祝你好运, 麦克

1 个答案:

答案 0 :(得分:1)

.cpp依赖于MFC,并且没用,因为它没有相应的类定义。如果要在项目中使用该算法,则应使用alphanum.hpp(它是一个仅限标题的库)。

要排序,请使用stl sort函数。

示例:

#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include "alphanum.hpp"

int main()
{
  std::vector<std::string> v;

  // fill array ...

  std::sort(v.begin(), v.end(), doj::alphanum_less<std::string>());

  //  v is sorted !

  return 0;
}