<legend id='mx4Za'><style id='mx4Za'><dir id='mx4Za'><q id='mx4Za'></q></dir></style></legend>

  • <small id='mx4Za'></small><noframes id='mx4Za'>

  • <tfoot id='mx4Za'></tfoot>

      <bdo id='mx4Za'></bdo><ul id='mx4Za'></ul>

    1. <i id='mx4Za'><tr id='mx4Za'><dt id='mx4Za'><q id='mx4Za'><span id='mx4Za'><b id='mx4Za'><form id='mx4Za'><ins id='mx4Za'></ins><ul id='mx4Za'></ul><sub id='mx4Za'></sub></form><legend id='mx4Za'></legend><bdo id='mx4Za'><pre id='mx4Za'><center id='mx4Za'></center></pre></bdo></b><th id='mx4Za'></th></span></q></dt></tr></i><div id='mx4Za'><tfoot id='mx4Za'></tfoot><dl id='mx4Za'><fieldset id='mx4Za'></fieldset></dl></div>

        为什么 std::accumulate 函数会显示向量的错误总和

        时间:2023-09-15
        <i id='cDLvk'><tr id='cDLvk'><dt id='cDLvk'><q id='cDLvk'><span id='cDLvk'><b id='cDLvk'><form id='cDLvk'><ins id='cDLvk'></ins><ul id='cDLvk'></ul><sub id='cDLvk'></sub></form><legend id='cDLvk'></legend><bdo id='cDLvk'><pre id='cDLvk'><center id='cDLvk'></center></pre></bdo></b><th id='cDLvk'></th></span></q></dt></tr></i><div id='cDLvk'><tfoot id='cDLvk'></tfoot><dl id='cDLvk'><fieldset id='cDLvk'></fieldset></dl></div>
          <tfoot id='cDLvk'></tfoot>

              <bdo id='cDLvk'></bdo><ul id='cDLvk'></ul>

              • <small id='cDLvk'></small><noframes id='cDLvk'>

                    <tbody id='cDLvk'></tbody>

                  <legend id='cDLvk'><style id='cDLvk'><dir id='cDLvk'><q id='cDLvk'></q></dir></style></legend>
                1. 本文介绍了为什么 std::accumulate 函数会显示向量的错误总和&lt;double&gt;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  考虑以下代码来添加 vector 的所有元素:

                  Consider the following code for adding all the elements of a vector:

                  #include<iostream>
                  #include<algorithm>
                  #include<numeric>
                  #include<vector>
                  using namespace std;
                  int main(void)
                  {
                  
                     std::vector<double> V;
                     V.push_back(1.2);
                     V.push_back(3.6);
                     V.push_back(5.6);
                     double sum = accumulate(V.begin(),V.end(),0);
                  
                     cout << "The sum of the elements of the vector V is " << sum << endl;
                     return 0;
                  }
                  

                  当我在 Windows 上的 Cygwin 上编译并运行它时,我在终端得到输出

                  When I compile this on Cygwin on Windows and run it, I get the output at the terminal as

                  向量V的元素之和为9

                  accumulate 函数似乎将所有数字四舍五入并将它们相加,这将解释答案.

                  The accumulate function seems to be rounding down all the numbers and adding them up, which would explain the answer.

                  这是 Cygwin g++ 编译器有问题,还是我对 accumulate 函数的错误解释,用于将 doublevector 相加?

                  Is this something wrong with the Cygwin g++ compiler, or my misinterpretation of the accumulate function for adding up a vector of doubles?

                  推荐答案

                  std::accumulate 声明如下:

                  template <typename InputIt, typename T>
                  T accumulate(InputIt first, InputIt last, T init);
                  

                  std::accumulate 的第二个模板参数被推导出为 int 因为 0int 类型.改为传递双精度值,例如 0.0.

                  The second template argument to std::accumulate is deduced as int because 0 is of type int. Pass a double instead, like 0.0.

                  这篇关于为什么 std::accumulate 函数会显示向量的错误总和&lt;double&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:空向量的开销成本是多少? 下一篇:STL 容器的内存消耗

                  相关文章

                  最新文章

                2. <i id='OMOyC'><tr id='OMOyC'><dt id='OMOyC'><q id='OMOyC'><span id='OMOyC'><b id='OMOyC'><form id='OMOyC'><ins id='OMOyC'></ins><ul id='OMOyC'></ul><sub id='OMOyC'></sub></form><legend id='OMOyC'></legend><bdo id='OMOyC'><pre id='OMOyC'><center id='OMOyC'></center></pre></bdo></b><th id='OMOyC'></th></span></q></dt></tr></i><div id='OMOyC'><tfoot id='OMOyC'></tfoot><dl id='OMOyC'><fieldset id='OMOyC'></fieldset></dl></div>

                  <tfoot id='OMOyC'></tfoot>

                  <small id='OMOyC'></small><noframes id='OMOyC'>

                    • <bdo id='OMOyC'></bdo><ul id='OMOyC'></ul>
                  1. <legend id='OMOyC'><style id='OMOyC'><dir id='OMOyC'><q id='OMOyC'></q></dir></style></legend>