我怎样才能在同一行得到我程序的所有总和?
How could I get all of the sums of my program on the same line?
我试图在程序结束时将所有输出打印在一行上。我怎样才能做到这一点?目前,输入变量后直接打印总和,看起来像这样:
3
100 8
108
15 245
260
1945 54
1999
我希望它看起来像这样:
3
100 8
15 245
1945 54
108 260 1999
这是我目前的代码:
#include <iostream>
using namespace std;
int main()
{
int pairs = 0;
cin >> pairs;
for (int i=0,num1=0,num2=0; i < pairs; i++)
{
cin >> num1 >> num2;
cout << num1 + num2 << " ";
}
}
起初,我不清楚你在问什么,但我明白了。您正在同一个循环中进行输入和输出。您需要有一个输入和输出循环以及一个容器:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
int main()
{
int pairs = 0;
cin >> pairs;
std::vector<int> sums; // vector to hold sums, your int sum was unused
sums.reserve(pairs);
for(int i = 0; i < pairs; ++i)
{
// better initialize these variables here, otherwise they might
// equal to previous input if this input fails
// (you should declare them in inner-most scope possible anyway)
int num1 = 0, num2 = 0;
cin >> num1 >> num2;
sums.push_back(num1 + num2); // do not cout, append the value to the sums instead
}
for(auto x : sums)
cout << x << " "; // finally print the whole vector
}
只是为了提供一个方便的替代方案:您可以将输出发送到一个 std::ostringstream
变量,直到您想要使用它...
#include <iostream>
#include <sstream>
int main()
{
int pairs;
if (std::cin >> pairs)
{
std::ostringstream saved_out;
for (int i = 0; i < pairs; ++i)
{
int num1, num2;
if (std::cin >> num1 >> num2)
saved_out << num1 + num2 << " ";
else
{
std::cerr << "ERROR: less inputs than promised\n";
exit(1);
}
}
}
else
{
std::cerr << "unable to parse pairs counter from stdin\n";
exit(1);
}
std::cout << saved_out.str() << '\n';
}
我还对输入进行了更多检查robust/verbose。
我试图在程序结束时将所有输出打印在一行上。我怎样才能做到这一点?目前,输入变量后直接打印总和,看起来像这样:
3
100 8
108
15 245
260
1945 54
1999
我希望它看起来像这样:
3
100 8
15 245
1945 54
108 260 1999
这是我目前的代码:
#include <iostream>
using namespace std;
int main()
{
int pairs = 0;
cin >> pairs;
for (int i=0,num1=0,num2=0; i < pairs; i++)
{
cin >> num1 >> num2;
cout << num1 + num2 << " ";
}
}
起初,我不清楚你在问什么,但我明白了。您正在同一个循环中进行输入和输出。您需要有一个输入和输出循环以及一个容器:
#include <iostream>
#include <vector>
using std::cout;
using std::cin;
int main()
{
int pairs = 0;
cin >> pairs;
std::vector<int> sums; // vector to hold sums, your int sum was unused
sums.reserve(pairs);
for(int i = 0; i < pairs; ++i)
{
// better initialize these variables here, otherwise they might
// equal to previous input if this input fails
// (you should declare them in inner-most scope possible anyway)
int num1 = 0, num2 = 0;
cin >> num1 >> num2;
sums.push_back(num1 + num2); // do not cout, append the value to the sums instead
}
for(auto x : sums)
cout << x << " "; // finally print the whole vector
}
只是为了提供一个方便的替代方案:您可以将输出发送到一个 std::ostringstream
变量,直到您想要使用它...
#include <iostream>
#include <sstream>
int main()
{
int pairs;
if (std::cin >> pairs)
{
std::ostringstream saved_out;
for (int i = 0; i < pairs; ++i)
{
int num1, num2;
if (std::cin >> num1 >> num2)
saved_out << num1 + num2 << " ";
else
{
std::cerr << "ERROR: less inputs than promised\n";
exit(1);
}
}
}
else
{
std::cerr << "unable to parse pairs counter from stdin\n";
exit(1);
}
std::cout << saved_out.str() << '\n';
}
我还对输入进行了更多检查robust/verbose。