为什么c ++ std accumulate总是返回0

问题描述 投票:-2回答:3

我试图使用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

c++ vector iterator accumulate
3个回答
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,了解保留与调整大小之间的差异。


0
投票

首先用你的东西填充你的向量,否则你可能会被定义

vector<int> x(3,0);

0
投票

使用c ++从输入中求和3个整数为什么累积总是返回0

这个答案使用了push_back(),并且不需要知道输入了多少个整数,因为向量将自动扩展;通过这种方式,它回避了std :: vector的问题,这些问题正在打败你的代码。

考虑一下,因为可能提交的“多少int”很少被修复,你更有可能想要计算“动态”输入的数量。所以也许使用循环,cin到局部var,然后x.push_back(a_local_var),并重复直到某些条件(可能是eof(),或局部var == -1等)x.size()是你的计数器。

这是一个功能实例,使用命令行变量和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

© www.soinside.com 2019 - 2024. All rights reserved.