使用 STL 列表获取最高值

Posted

技术标签:

【中文标题】使用 STL 列表获取最高值【英文标题】:using the STL list to get highest value 【发布时间】:2016-08-29 12:59:58 【问题描述】:

编写一个 C++ 程序,要求用户输入一个整数 m,然后输入 m 个其他学生的姓名和代表他们最终成绩的数字(满分 100)。每次,用户都必须输入姓名和成绩。姓名和成绩将存储在单独的列表中。

在获取所有姓名和成绩后,程序会查找并显示最高成绩和拥有该成绩的学生的姓名

我试过了,能拿到最高分,但是名字我写错了……

plzzzzzzzzzzzzzzzzzzzzz 帮助!!!!!!

#include <cstdlib>
#include<iostream>
#include<string>
#include<list>
using namespace std;

int main() 
    list<string>names;
    list<int>grades;
    int m, grade;
    string studentname;
    cout << "Enter m names and m grades \n";
    cin>>m;
    for (int i = 0; i < m; i++) 
        cout << "Enter students name " << i + 1 << ":" << endl;
        cin>>studentname;
    names.push_back(studentname);
        cout << "Enter students grade " << i + 1 << ":" << endl;
        cin>>grade;
    grades.push_back(grade);
    
   list<string>::iterator tn; //iterator tn to read a list of names
   list<int>::iterator tg;  //iterator tg to read a list of grades
   float highest;
   string name;
   tn = names.begin(); //to point to the first name
   tg = grades.begin(); //to point to the first grade
   highest = *tg; //suppose that the highest grade is the first grade
   name = *tn;   //suppose that the first student has the highest grade
   tg++;  //to move to the next grade
   tn++;  //to move to the next name
   for (tg; tg != grades.end(); tg++) 
      if (highest<*tg) 
          highest=*tg;
          grades.pop_back();
      
   tn++; //to read in the list of students’ names
   
   cout << "----------\n";
   cout << "Highest grade: " << highest << " for: " << name;
   cout << "\n----------\n";
   return 0;

【问题讨论】:

如果没有std::max_element/std::distance,我会使用std::vector 和索引。 grades.pop_back(); 可疑 【参考方案1】:

您在循环之前将name 设置为name = *tn;,以后再也不更改它。你期待什么?

【讨论】:

这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review 这并没有提供问题的答案,因为原始帖子中没有问题。几乎可以肯定,隐含的问题是“为什么我的程序会这样表现”​​。我提供了答案。【参考方案2】:

此答案仅供您参考。

虽然我不建议您将此代码提交给您的导师(但一定要在课后与他/她讨论),但这是我“在现实世界中”处理此任务的方式。

简介:

所有算法逻辑都用标准算法表示。

出于效率考虑将列表替换为向量。

内置测试工具允许使用 --test 选项运行程序以检查逻辑,并为此...

逻辑与 IO 流分离。

输入不完整或无效时的错误处理。

使用模板函数获取输入,无需重复逻辑。

此代码需要 c++11,这应该是您正在学习的最低要求。

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>

template<class...Ts>
std::istream& acquire(std::istream& stream, const char* prompt, Ts&...ts)

    if (std::addressof(stream) == std::addressof(static_cast<std::iostream&>(std::cin))) 
        std::cout << prompt;
        std::cout.flush();
    
    using expand = int[];
    void(expand  0, ((stream >> ts),0)... );
    return stream;


bool args_have_test(const std::vector<std::string>& args)

    auto ifind = std::find(std::begin(args), std::end(args), "--test");
    return ifind != std::end(args);


[[noreturn]]
bool input_error(const char* context)

    std::cerr << "input error while " << context << std::endl;
    exit(2);



int main(int argc, const char* const *argv)

    std::istringstream stest 
        "5\n"
        "bob 5\n"
        "bill 2\n"
        "bernie 9\n"
        "bert 7\n"
        "bart 8\n"
    ;
    auto args = std::vector<std::string>  argv + 1, argv + argc ;
    auto& stream = args_have_test(args) ? static_cast<std::istream&>(stest)
    : static_cast<std::istream&>(std::cin);
    int count = 0;
    acquire(stream, "enter number of students: ", count)
    or input_error("entering number of students");

    std::vector<std::string> names;
    names.reserve(count);
    std::vector<int> grades;
    grades.reserve(count);
    std::string name;
    int grade;
    while (count--) 
        acquire(stream, "enter name and grade followed by enter: ", name, grade)
        or input_error("entering name and grade");

        names.push_back(name);
        grades.push_back(grade);
    
    auto imax = std::max_element(std::begin(grades), std::end(grades));
    if (imax == std::end(grades)) 
        std::cerr << "empty list\n";
        exit(1);
    
    auto iname = std::next(std::begin(names), std::distance(std::begin(grades),
                                                            imax));
    std::cout << "highest grade was " << *imax << " acheived by " << *iname << std::endl;
    return 0;

【讨论】:

以上是关于使用 STL 列表获取最高值的主要内容,如果未能解决你的问题,请参考以下文章

Gremlin 从与单个顶点相关的边列表中获取最高值

为啥我在使用 STL 列表时不能使用这个回文函数?

从 stl_load_commits 获取 Redshift COPY 命令的表名

在 C++ 中使用 stl 列表和类

STL 从列表中删除

在不使用 STL 的情况下,在 C++ 中存储和实例化类列表的最佳方法是啥?