为什么 c++ std accumulate 总是 return 0
why c++ std accumulate is always return 0
我正在尝试使用 C++ 对输入的 3 个整数求和,但我一直得到 0。请帮忙,谢谢。
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
vector::reserve(size_type n)
将请求更改向量的容量,而不是大小。您可以使用调整大小功能,或者更好的构造函数。
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了解保留与调整大小之间的区别。
首先用一些东西填充你的向量,否则你可能会得到未定义的
vector<int> x(3,0);
use c++ to sum 3 integers from the input
why accumulate always return 0
这个答案使用了push_back(),并且不需要知道输入了多少个整数,因为向量会自动扩展;通过这种方式,它回避了 std::vector 破坏您的代码的问题。
请考虑一下,因为提交的 "how many int" 很少是固定的,所以您更有可能想要计算输入的数量 "on the fly"。所以也许使用循环,cin 到局部变量,然后 x.push_back(a_local_var),并重复直到某些条件(可能是 eof(),或局部变量 == -1 等)x.size() 是你的计数器。
这是一个功能性示例,使用命令行 vars 和 eof()(以及向量和 accumulate)。
// 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 后失败,因为最后一个整数太大
./dumy951 1 2 3 y 55 12345678900 <-- 在 int 值 3 之后失败(无效 int)
./dumy951 1 2 3 4 5 6 7 8 9 10 <-- 成功,结果为 55
我正在尝试使用 C++ 对输入的 3 个整数求和,但我一直得到 0。请帮忙,谢谢。
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
vector::reserve(size_type n)
将请求更改向量的容量,而不是大小。您可以使用调整大小功能,或者更好的构造函数。
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了解保留与调整大小之间的区别。
首先用一些东西填充你的向量,否则你可能会得到未定义的
vector<int> x(3,0);
use c++ to sum 3 integers from the input why accumulate always return 0
这个答案使用了push_back(),并且不需要知道输入了多少个整数,因为向量会自动扩展;通过这种方式,它回避了 std::vector 破坏您的代码的问题。
请考虑一下,因为提交的 "how many int" 很少是固定的,所以您更有可能想要计算输入的数量 "on the fly"。所以也许使用循环,cin 到局部变量,然后 x.push_back(a_local_var),并重复直到某些条件(可能是 eof(),或局部变量 == -1 等)x.size() 是你的计数器。
这是一个功能性示例,使用命令行 vars 和 eof()(以及向量和 accumulate)。
// 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 后失败,因为最后一个整数太大
./dumy951 1 2 3 y 55 12345678900 <-- 在 int 值 3 之后失败(无效 int)
./dumy951 1 2 3 4 5 6 7 8 9 10 <-- 成功,结果为 55