C ++ std :: sort函数没有完成?

时间:2018-04-23 13:38:47

标签: c++ arrays string sorting

我目前正在设置游戏的高分部分,由于std :: sort函数的奇怪行为,我有一个非常奇怪的问题。

我正在使用C ++在RAD Studio 10.2(Embarcadero IDE)中完成所有工作。

所以他是我的代码:

std::string Line;
int count = 0;
int i = 0;
ifstream File("Highscore.txt");
if(File.is_open())
{
    while(getline(File, Line))
    {
        count += 1;

    }

    File.close();

}

ifstream ReadFile("Highscore.txt");
if(ReadFile.is_open())
{
    string *scores = NULL;
    scores = new string[count];

    while(getline(ReadFile, Line))
    {
        scores[i] = Line;
        i += 1;
    }

    ReadFile.close();


    std::sort(scores, (scores+count));

    UnicodeString Uscores1 = scores[0].c_str();
    UnicodeString Uscores2 = scores[1].c_str();
    UnicodeString Uscores3 = scores[2].c_str();
    UnicodeString Uscores4 = scores[3].c_str();
    UnicodeString Uscores5 = scores[4].c_str();
    LScore1->Caption = Uscores1;
    LScore2->Caption = Uscores2;
    LScore3->Caption = Uscores3;
    LScore4->Caption = Uscores4;
    LScore5->Caption = Uscores5;

}

我没有从编译器/链接器中得到任何错误,一切正常。 字符串数组被正确填充,依此类推。

但它没有排序。

为了向你展示问题我做了一个截图 - 在左边你可以看到带有分数的txt文件;在右侧,您可以看到排序算法后的输出:

enter image description here

我现在的问题是为什么会这样?

谢谢你的帮助

3 个答案:

答案 0 :(得分:3)

欢迎使用C ++。由于您要按排名列出数字,请将其读作int而不是string。忘记运算符new。如果有的话,你将不会需要它多年。使用像std::vector这样的标准容器,它们可以透明地处理内存分配和解除分配。

#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
int main() {
    using namespace std;
    vector<int> scores;
    {
        ifstream inp("Highscore.txt");
        int next;
        while (inp >> next) {
            scores.push_back(next);
        }
    }
    sort(scores.begin(), scores.end());
    for (auto s : scores) {
        cout << s << '\n';
    }
    return 0;
}

答案 1 :(得分:0)

如下:

int i = 0;
int * scoresInteger = NULL;
scoresInteger = new int[count];
for(i = 0; i < count; i++)
{
    scoresInteger[i] = std::stoi(scores[i]);
}
std::sort(scoresInteger, scoresInteger + count);

如果需要,可以使用targetStrings[i] = std::to_string(scoresInteger[i])将整数转换回字符串。

string * targetScores = NULL;
targetScores = new std::string[count];
for(i = 0; i < count; i++)
{
    targetScores[i] = std::to_string(scoresInteger[i]);
}
delete [] scoresInteger;
scoresInteger = NULL;

以后不要忘记delete [] targetScores

答案 2 :(得分:0)

  

我现在的问题是为什么会这样?

因为您的分数被比较为string而不是int。因为“3”大于“25”

std::cout << std::boolalpha << (std::string("3") > std::string("25")) << std::endl; // true

幸运的是,您可以将自定义比较器(或lambda)传递给std::sort,使其行为符合您的要求:

#include <iostream>
#include <string>
#include <algorithm>

int main()
{
    const int count = 5;
    std::string scores[count] = { "35","25","3","4","5" };

    // TWEAKED SORT
    std::sort(scores, scores + count, [](std::string const &s1, std::string const &s2)
    {
        return std::stoi(s2) < std::stoi(s1);
    });

    // TEST
    for (auto const &s : scores)
    {
        std::cout << s << std::endl;
    }
}

上例中的比较string转换为int,然后进行比较,得到desired sorting order

35
25
5
4
3

请注意,我不同意您的其他代码,我认为您应该重新考虑实施,因为使用std::vector<std::string>来完成任务会更容易,更安全,更有效。

相关问题