我正在尝试使用c ++从输入中求和3个整数,但我一直保持为0。请帮助thx。
vector<int> x;
x.reserve(3);
cin >> x[0] >> x[1] >> x[2];
int sum = std::accumulate(x.begin(), x.end(), 0);
cout << sum << endl;
return 0;
1
2
3
0
答案 0 :(得分:1)
vector::reserve(size_type n)
将请求更改向量的容量,而不是大小。您可以使用resize函数,甚至可以使用更好的构造函数。
int main()
{
std::vector<int> x(3,0); //set the size to 3 and fill with zeros.
std::cin >> x[0] >> x[1] >> x[2];
int sum = std::accumulate(x.begin(), x.end(), 0);
std::cout << sum << std::endl;
}
您可以阅读此答案here,了解保留大小和调整大小之间的区别。
答案 1 :(得分:0)
如果不先填充矢量,可能会变得不确定
vector<int> x(3,0);
答案 2 :(得分:0)
使用c ++从输入求和3个整数 为什么累积总是返回0
此答案使用push_back(),并且不需要知道输入了多少个整数,因为向量将自动展开;这样,它就可以避免破坏代码的std :: vector问题。
请考虑一下,因为很少会固定提交“多少个int”,所以您更有可能想要“实时”计算多少个输入。因此,也许使用一个循环,cin到一个本地变量,然后是x.push_back(a_local_var),然后重复直到某些条件(也许是eof()或local var == -1等)x.size()是您的计数器
这是一个使用命令行vars和eof()(以及向量和累加)的示例。
// Note: compile with -std=c++17 for the using comma list
#include <iostream>
using std::cout, std::cerr, std::endl, std::hex, std::dec, std::cin, std::flush; // c++17
#include <vector>
using std::vector;
#include <string>
using std::string;
#include <sstream>
using std::stringstream;
#include <numeric>
using std::accumulate;
#include <cassert>
class T951_t // ctor and dtor compiler provided defaults
{
public:
int operator()(int argc, char* argv[]) { return exec(argc, argv); } // functor entry
private:
stringstream ssIn; // to simulate user input
int exec(int argc, char* argv[])
{
int retVal = initTest(argc, argv); // transfer command line strings into ssIn
if(retVal != 0) return retVal;
// ------------------------------------------------------------
// simulate unknown quantity of ints
vector<int> x;
do {
int localInt = 0;
ssIn >> localInt;
if(!ssIn.good()) // was transfer ok?
{ // no
if (ssIn.eof()) break; // but we tolerate eof
// else err and exit
cerr << "\n !ssIn.good() failure after int value "
<< x.back() << endl;
assert(0); // harsh - user typo stops test
}
x.push_back(localInt); // yes transfer is ok, put int into vector
//cout << "\n " << localInt; // diagnostic
} while(true);
showResults(x);
return 0;
}
// this test uses a stringstream (ssIn) to deliver input to the app
// ssIn is initialized from the command line arguments
int initTest(int argc, char* argv[])
{
if (argc < 2) {
cerr << "\n integer input required" << endl;
return -1;
}
// test init
for (int i=1; i < argc; ++i) {
// cout << "\n " << argv[i]; // diagnostic
ssIn << argv[i] << " "; // user text into stream
}
cout << endl;
return 0;
}
// display size and contents of vector x
void showResults(vector<int> x)
{
cout << "\n x.size(): " << x.size() << endl;
int sum = std::accumulate(x.begin(), x.end(), 0);
for (auto i : x)
cout << " " << i;
cout << endl;
cout << "\n sums to: " << sum << '\n' << endl;
}
}; // class T951_t
int main(int argc, char* argv[]) { return T951_t()(argc, argv); } // call functor
测试:
./ dumy951 1 2 3 55 12345678900 <-55后失败,因为最后一个int太大
./ dumy951 1 2 3 y 55 12345678900 <-在int值3(无效int)之后失败
./ dumy951 1 2 3 4 5 6 7 8 9 10 <-成功,结果为55