C++ C++;排序函数没有完成?

C++ C++;排序函数没有完成?,c++,arrays,string,sorting,C++,Arrays,String,Sorting,我目前正在为一个游戏设置highscore部分,我有一个非常奇怪的问题,因为std::sort函数的行为很奇怪 我在C++的RAD Studio 10.2(ActuCabeRoide)中做了这件事。 所以他是我的密码: std::string Line; int count = 0; int i = 0; ifstream File("Highscore.txt"); if(File.is_open()) { while(getline(File, Line)) {

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

我在C++的RAD Studio 10.2(ActuCabeRoide)中做了这件事。 所以他是我的密码:

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文件;在右侧,您可以看到排序算法后的输出:

我现在的问题是为什么会发生这种情况


谢谢你的帮助

像这样的东西怎么样:

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);

以后别忘了删除[]targetScores。

类似于:

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);
以后不要忘记删除[]targetScores

我现在的问题是为什么会发生这种情况

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

上例中比较的
字符串
s被转换为
int
s,然后进行比较,结果为

请注意,我不同意您的代码的其余部分,我认为您应该重新考虑实现,因为在您的任务中使用
std::vector
将更容易、更安全、更高效

我现在的问题是为什么会发生这种情况

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

上例中比较的
字符串
s被转换为
int
s,然后进行比较,结果为


请注意,我不同意你的代码,我认为你应该重新考虑这个实现,因为使用<代码> STD::向量对你的任务将是更容易、更安全和更有效的。

欢迎来到C++。由于要按排名列出数字,请将其读作

int
而不是
string
。忘记操作员
新建
。如果有可能的话,你几年内都不会需要它。使用标准容器,如
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;
}
#包括
#包括
#包括
#包括
int main(){
使用名称空间std;
向量得分;
{
ifstream inp(“Highscore.txt”);
int-next;
while(inp>>下一步){
得分。推回(下一步);
}
}
排序(scores.begin()、scores.end());
对于(自动s:分数){

CUT< P>欢迎C++,因为你想按等级列出数字,把它们当作“代码> int <代码> >不要<代码>字符串< /C>。忘记操作符<代码>新< /Cord>。如果你曾经使用过,你就不需要它了。使用标准的容器,比如“代码> STD::vector < /Cord>”,它透明地处理内存分配和分配。
#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;
}
#包括
#包括
#包括
#包括
int main(){
使用名称空间std;
向量得分;
{
ifstream inp(“Highscore.txt”);
int-next;
while(inp>>下一步){
得分。推回(下一步);
}
}
排序(scores.begin()、scores.end());
对于(自动s:分数){


不能排序。如果你想按数字排序,你应该使用数字类型。如果以前没有人告诉你,请查看我们的页面。不要发布数据截图,将其硬编码到你的示例中。我错了,在发布几秒钟后删除了我的评论。当我喜欢示例中的5分时,我的TXT文件和输出scores[4]输出为5=我的TXT文件中的第五项。@Samydress您有5分和2“空”行,它们都是
std::string
的有效值。请检查
count
的值,它会进行排序。如果要进行数字排序,应使用数字类型。如果以前没有人告诉过您,请查看我们的页面。不要发布数据的屏幕截图,请将其硬编码到示例中。我错了,删除了我的在发布后的几秒钟内发表评论。当我在我的TXT文件中有如示例中所示的5分并输出分数[4]时,输出为5=我的TXT文件中的第五项。@samydress你有5分,2“空”行,它们都是
std::string
的有效值。检查
count
的值,请
delete
分配数组时的内存。复制此代码将导致内存泄漏。@PetarVelev或您知道的…使用std::vectorPlease
删除
分配数组时的内存“复制这个代码会导致内存泄漏。”PATARVELV或你知道……使用STD::VoCress你能告诉我为什么你否决了我的答案吗?谢谢你,这对我帮助很大。它给了我一个理由来解释C++和容器:D在我的大学里用C++开发一年软件,没有人提到过。学生们被教导要用不易出错的方式做事情,但在现实世界的代码审查中是完全不可接受的。请你给我解释一下你为什么拒绝我的答案的原因?谢谢你,这对我帮助很大。C++让我有理由去了解C++中的C++和容器:D,在我的大学里没有一个C++开发的软件。没有人提到过向量之类的东西。这是悲哀的:但是实际上是典型的。几十年来,C++变得容易做。学生被教做的事情不仅是错误的,而且是完整的。你(虽然我不知道你为什么认为Jive应该负责;他的回答同样可能引起了人们对这个问题的注意,随后的访问者投了反对票),我可以给你至少一个投反对票的理由(我没有,但这是一个足够大的错误)
35
25
5
4
3
#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;
}