使用STL列表获取最高值 编写一个C++程序,要求用户输入整数M,后面跟着M的其他学生姓名和数字,表示100的最终成绩。每次,用户都必须输入名称和等级。姓名和等级将存储在单独的列表中
在获得所有的名字和分数后,程序将查找并显示最高的分数和拥有最高分数的学生的名字 我试过了,并且得到了最高的分数,但是我的名字错了 请帮帮忙使用STL列表获取最高值 编写一个C++程序,要求用户输入整数M,后面跟着M的其他学生姓名和数字,表示100的最终成绩。每次,用户都必须输入名称和等级。姓名和等级将存储在单独的列表中,c++,list,data-structures,iterator,C++,List,Data Structures,Iterator,在获得所有的名字和分数后,程序将查找并显示最高的分数和拥有最高分数的学生的名字 我试过了,并且得到了最高的分数,但是我的名字错了 请帮帮忙 #include <cstdlib> #include<iostream> #include<string> #include<list> using namespace std; int main() { list<string>names; list<int>gra
#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;
int main(){
名单名称;
名单职系;
int m,等级;
字符串studentname;
cout>m;
for(int i=0;i 你可以在循环之前将名称设置为名称=*tn;
,以后再也不要更改它。你期望得到什么?你可以在循环之前将名称设置为名称=*tn;
,以后再也不要更改它。你期望得到什么?这个答案只是为了你的兴趣
虽然我不建议你将此代码提交给你的导师(但一定要在课后与他/她讨论),但这就是我“在现实世界”处理此任务的方式
简介:
- 用std算法表示的所有算法逻辑
- 基于效率的原因,列表替换为向量
- 内置测试线束允许程序使用--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;
}
#包括
#包括
#包括
#包括
#包括
模板
std::istream&acquire(std::istream&stream,常量字符*提示符,Ts&…Ts)
{
if(std::addressof(stream)=std::addressof(static_cast(std::cin))){
std::cout>ts),0)…});
回流;
}
布尔参数有测试(常量标准::向量和参数)
{
自动ifind=std::find(std::begin(args),std::end(args),“--test”);
返回ifind!=std::end(args);
}
[[noreturn]]
布尔输入_错误(常量字符*上下文)
{
这个答案只是为了你的兴趣
虽然我不建议你将此代码提交给你的导师(但一定要在课后与他/她讨论),但这就是我“在现实世界”处理此任务的方式
简介:
- 用std算法表示的所有算法逻辑
- 基于效率的原因,列表替换为向量
- 内置测试线束允许程序使用--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;
}
#包括
#包括
#包括
#包括
#包括
模板
std::istream&acquire(std::istream&stream,常量字符*提示符,Ts&…Ts)
{
if(std::addressof(stream)=std::addressof(static_cast(std::cin))){
std::cout>ts),0)…});
回流;
}
布尔参数有测试(常量标准::向量和参数)
{
自动ifind=std::find(std::begin(args),std::end(args),“--test”);
返回ifind!=std::end(args);
}
[[noreturn]]
布尔输入_错误(常量字符*上下文)
{
如果没有std::max_元素
/std::distance
,我会使用std::vector
和索引。grades.pop_back();
是可疑的如果没有std::max_元素
/std::distance
,我会使用std::vector
和索引。grades grades.pop_back()
可疑这并没有提供问题的答案。若要评论或要求作者澄清,请在其帖子下方留下评论。-这并没有提供问题的答案,因为原始帖子中没有问题。几乎可以肯定的是,隐含的问题是“为什么我的程序会这样做?”。我提供答案。这并不能回答问题。若要评论或要求作者澄清,请在其帖子下方留下评论。-这并不能回答问题,因为原始帖子中没有问题。几乎可以肯定,隐含的问题是“为什么我的程序会这样运行”,我给出了答案。