C++ 如何显示与某个变量关联的名称?…请澄清此部分

C++ 如何显示与某个变量关联的名称?…请澄清此部分,c++,stack,C++,Stack,这里有一个名字列表,这些名字有GPA,准确地说是7。我有获得最高GPA的算法,我知道有一些while循环条件可以用来获得GPA最高的人的名字 从问题中可以看出,两个人的平均成绩都是一样的。。。我将如何分别显示这两个GPA最高的名称?我似乎找不到能把名字和GPA联系起来的那部分 非常感谢您的帮助 编辑:在我的代码中,是否有一种更简单的方法可以在for循环的某个地方使用while循环?如果我使用if-gpa

这里有一个名字列表,这些名字有GPA,准确地说是7。我有获得最高GPA的算法,我知道有一些while循环条件可以用来获得GPA最高的人的名字

从问题中可以看出,两个人的平均成绩都是一样的。。。我将如何分别显示这两个GPA最高的名称?我似乎找不到能把名字和GPA联系起来的那部分

非常感谢您的帮助

编辑:在我的代码中,是否有一种更简单的方法可以在for循环的某个地方使用while循环?如果我使用if-gpa
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main()
{
    string name;
    double gpa;
    double high = 0;

    stack<string>names;

    for (int i=0; i<7; i++)
    {
        cout << " Enter student's name and gpa " << endl;
        cin >> gpa ;
        cin >> name ;
    }

    { 
        if (gpa > high)
        {
            high = gpa;
            names.push(name);
        }
        else if (gpa=high)
        {
            high = gpa;
            names.push(name);
        }
    }

    cout << "Highest gpa is"<< high << "Names with highest gpa "<< ??? <<< endl;
    system("pause");

    return 0;
}
然后使用std::priority_队列存储它,并确保创建一个比较运算符

也可以使用==表示相等,而不是=这是赋值

尝试以下方法:

struct Data {
 std::string name;
 int gpa;
};

你在else-if中的条件被破坏了。什么是破坏了?这意味着如果gpa等于最高gpa,我想用这个gpa来显示名字。但是还有其他正确的代码吗?@Surya:他指出你拥有的是一个赋值,而不是一个比较。@Surya,如果gpa=高,则else应为如果gpa=高,则elsehigh@Surya-我已经纠正了导致许多标志的印刷错误。请像与专业同龄人交流一样构建句子,不是你在发短信。我想在不使用链接列表的情况下这样做。我缺少了一些更简单的代码…在if gpa#include <iostream> #include <stack> #include <string> using namespace std; int main() { string name; double gpa; double high = 0; stack<string>names; for (int i=0;i<7;i++) { cout << " Enter student's name and gpa " <<endl; cin >> gpa; cin >> name; if (gpa > high) { high = gpa; names.clear(); names.push(name); } else if (gpa == high) { names.push(name); } } cout << "Highest gpa is "<< high << " held by:" << endl; for (stack<string>::const_iterator it(names.begin()); it != names.end(); ++it) { cout << *it << endl; } system("pause"); return 0; }