在我的家庭作业中,我的老师要我做一个程序,该程序计算谁的债务最多,他很遗憾输入应该在一行上,例如:
安德鲁4
彼得5
这是我的问题:如何在用空格分隔的一行上输入字符串和整数值。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
//n is the number of names and k is number of how many debts i can delete but dont bother with that
int n,k,SizeDebt;
string str1;
vector <string> Names;
vector <int> Debt;
do
{
cin >> n >> k;
} while (n<1 || k>1000000);
for (int i = 0; i < n; i++)
{
getline(cin, str1);
cin >> SizeDebt;
Names.push_back(str1);
Debt.push_back(SizeDebt);
}
cin.get();
}
答案 0 :(得分:0)
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <string>
#include <utility>
int main()
{
using namespace std;
pair<size_t, string> max_debt {};
size_t n, k, debt {};
string name;
// use k
cin >> n >> k;
// Integral types (Boolean, Character and Integer) implicitly converts to the bool.
// For integrals that aren't equal to 0 result of the conversion is true, else - false.
// So, when we write --n, that means to compare whether n is equal to zero, and then decrement n.
while (n-- && cin >> name && cin >> debt) // answer
if (debt > max_debt.first)
max_debt = make_pair(debt, name);
cout << max_debt.second << " has max debt – " << max_debt.first << endl;
return EXIT_SUCCESS;
}