是否可以从此代码中打印出特定的行? [英] Is it possible to print specific lines out of this code?

查看:43
本文介绍了是否可以从此代码中打印出特定的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印程序中需要的所有内容.它的作用是从文本文件中提取一长串列表,并根据首选和GPA对其进行排序,然后将其放入向量中.

I am trying to print out whatever is necessary from my program. What it does is it takes a long list from a text file and sort it based on first choice and GPA and put it into a vector.

我设法按 First 选择和 GPA 进行排序,但是如何删除不需要的输出呢?

I manage to sort by First choice and GPA however how can I remove whatever output that isn't necessary?

我知道我之前曾问过这个问题,但我认为以前并没有正确询问过,并且我已经对其进行了编辑.

I know I asked this before but I think didn't ask correctly previously and I already edited some of it.

这是我的Txt文件的示例(每行的顺序是第一选择,第二选择,第三选择,GPA,名称):

This is an example of my Txt File (The sequence of each line is 1st choice, 2nd choice, 3rd choice, GPA, Name):

CC,DR,TP,3.8,AlexKong
SN,SM,TP,4,MarcusTan
DR,TP,SC,3.6,AstaGoodwin
SC,TP,DR,2.8,MalcumYeo
SN,SM,TP,3.7,DavidLim
SN,SM,TP,3.2,SebastianHo
SC,TP,DR,4,PranjitSingh
DR,TP,SC,3.7,JacobMa
and so on...

这是我的输出(这是一个很长的向量):

This is my output now (it is a long vector):

TP,DR,SC,4,SitiZakariah
TP,DR,SC,3.9,MuttuSami
TP,DR,SC,3.5,SabrinaEster
TP,DR,SC,3,KarimIlham
TP,DR,SC,3,AndryHritik
SN,SM,TP,4,JasonTan
SN,SM,TP,3.8,MarcusOng
SN,SM,TP,3.7,DavidLim
SN,SM,TP,3.4,MollyLau
SN,SM,TP,3.2,SebastianHo
SN,SM,TP,3.2,NurAfiqah
SN,SM,TP,2.4,TanXiWei
SC,TP,DR,4,SallyYeo
SC,TP,DR,4,PranjitSingh
SC,TP,DR,3.6,RanjitSing
SC,TP,DR,2.8,MalcumYeo
SC,TP,DR,2.8,AbdulHalim
SC,TP,DR,2.7,AlifAziz
DR,TP,SC,3.9,SitiAliyah
DR,TP,SC,3.9,LindaChan
DR,TP,SC,3.8,SohLeeHoon
DR,TP,SC,3.7,PrithikaSari
DR,TP,SC,3.7,NurAzizah
DR,TP,SC,3.7,JacobMa
DR,TP,SC,3.6,AstaGoodwin
CC,DR,TP,3.9,MuruArun
CC,DR,TP,3.7,DamianKoh
CC,DR,TP,3.3,MattWiliiams
CC,DR,TP,3.3,IrfanMuhaimin

这是我需要的输出(基本上是将 CC 作为其第一选择的学生,而不显示3个选项.我不希望其他没有 CC 的选项作为他们的第一个选择.我已经设法在没有以下3个选择的情况下打印输出了.):

And this is the output that I need (Basically students with CC as their 1st choice without displaying the 3 options. I don't want the other options without CC as their first option. I already manage to print the output without the 3 choices as follow.):

3.9,MuruArun
3.8,AlexKong
3.7,DamianKoh
3.3,MattWiliiams
3.3,IrfanMuhaimin

这是我的程序:

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

struct greater
{
    template<class T>
    bool operator()(T const &a, T const &b) const { return a > b; }
};

void main()
{
    vector<string> v;
    ifstream File;
    File.open("DSA.txt");
    if (!File.is_open()) return;

    string line;
    string Name;
    string GPA;
    string First;
    string Second;
    string Third;

    getline(File, First, ',');
    getline(File, Second, ',');
    getline(File, Third, ',');
    getline(File, Name, ',');
    getline(File, GPA, '\n');

    cout << "Round 1:\n";

    if (First == "CC")
        while (File>>line)
        {
            v.push_back(line);
        }
        sort(v.begin(), v.end(), greater());
        for (int i = 0; i < v.size(); i++)
        {
            cout << v[i].substr(9) << endl; //remove first 3 choices from output
        }
}

这是我尝试过滤输出的尝试:

This is my attempt to filter out my output:

if (First == "CC")
        while (File>>line)
        {
            v.push_back(line);
        }
        sort(v.begin(), v.end(), greater());
        for (int i = 0; i < v.size(); i++)
        {
            cout << v[i].substr(9) << endl;
        }

我认为,如果我获得getline并设置if条件以分隔CC(如果第一个选择是CC,则条件为true),那么我只会打印以CC作为第一选择的那些,而忽略其余的内容.因此,基本上,我尝试将CC作为首选.但是显然我错了.所以我希望有人知道如何过滤输出

I thought that if I getline and make an if condition to separate CC (if the first choice is CC, then condition is true) then I only print the ones with CC as first choice and ignore the rest. so basically I try to search for CC as the first choice. But obviously I was very wrong. So I was hoping if anyone knows how to filter the output

推荐答案


上一点:


Previous point:

正如注释部分 using namespace std; 所指出的那样,这是一个错误的选择,您的代码中有一个示例,说明了为什么重新定义更大的原因之一>已经存在于名称空间中.

As was noted in the comment section using namespace std; is a bad choice and your code has an example of one of the reasons why that is, the redefinition of greater which is already present in the namespace.

提供的链接具有进一步的说明和替代方法.

The provided link has further explanation and alternatives.

对于您的代码,据我所知,如果目标是从 CC 开始不带选项的行(按 GPA 排序),则更简单例如,您可以使用 std :: find 只能解析开头为"CC" 的行,然后从那里开始工作.
您还可以使用 std :: string :: starts_with ,但是它仅在C ++ 20中可用,因此我将选择第一个选项.

As for you code, if the goal is to output the lines, starting with CC without the options, ordered by GPA, as I understand it, there are simpler ways of doing it, for instance, you can use std::find to parse only lines with "CC" at its beginning and work from there.
You could also use std::string::starts_with however it's only available with C++20, so I'll go with the first option.

实时演示

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

    std::ifstream File;
    File.open("DSA.txt");
    if (!File.is_open())
        return EXIT_FAILURE;

    std::string line;

    while (File >> line)
    {
        if (line.find("CC") == 0)    // find lines that start with CC
            v.push_back(&line[9]);   // add lines without the options
    }                                // or v.push_back(line.substr(9));

    sort(v.begin(), v.end(), std::greater<std::string>()); //sort the lines

    std::cout << "GPA" << "\t" << "Name" <<"\n\n"; // Title for the table
    for (auto& str : v) //print the lines
    {   
        std::replace(str.begin(), str.end(), ',', '\t'); //let's replace de comma
        std::cout << str << "\n";
    }
    return EXIT_SUCCESS;
}

获取样本,将输出:

GPA    Name

3.9    MuruArun
3.7    DamianKoh
3.3    MattWiliiams
3.3    IrfanMuhaimin

带有"CC"的行第二个或第三个选项将不会被解析,这是我们的目标.

Lines with "CC" in second or third options will not be parsed, as is our goal.

注意:

这种按字符串的排序方法是可行的,并且在这种情况下可以使用,因为 GPA 的值小于10,否则我们将不得不转换 GPA 字段并对字符串进行排序按其值 eg 排列:10大于9,但作为字符串将首先排序,因为按字典顺序排序,9被认为较大,字符9较大比字符1.

This sorting method by string is possible and works in this case because the GPA values are lower than 10, otherwise we would have to convert the GPA field and sort the lines by its value, e.g.: 10 is larger than 9 but as a string it would be sorted first because lexicographicaly ordered, 9 would be considered larger, the character 9 is larger than the character 1.

如您所见,我使用了默认的 更大 模板,在这种情况下,您无需自己制作模板,只需使用此模板即可.

As you can see I used the default greater template, in this case you don't need to make your own, you can just use this one.

还有一件事, main 必须具有 int 返回类型.

One more thing, main must have int return type.

这篇关于是否可以从此代码中打印出特定的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆