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

  • <i id='VqG8C'><tr id='VqG8C'><dt id='VqG8C'><q id='VqG8C'><span id='VqG8C'><b id='VqG8C'><form id='VqG8C'><ins id='VqG8C'></ins><ul id='VqG8C'></ul><sub id='VqG8C'></sub></form><legend id='VqG8C'></legend><bdo id='VqG8C'><pre id='VqG8C'><center id='VqG8C'></center></pre></bdo></b><th id='VqG8C'></th></span></q></dt></tr></i><div id='VqG8C'><tfoot id='VqG8C'></tfoot><dl id='VqG8C'><fieldset id='VqG8C'></fieldset></dl></div>
      <bdo id='VqG8C'></bdo><ul id='VqG8C'></ul>
  • <legend id='VqG8C'><style id='VqG8C'><dir id='VqG8C'><q id='VqG8C'></q></dir></style></legend>

        <tfoot id='VqG8C'></tfoot>

        如何总结 C++ 向量的元素?

        时间:2023-09-15
          <tbody id='hKfZ4'></tbody>
      1. <small id='hKfZ4'></small><noframes id='hKfZ4'>

        <i id='hKfZ4'><tr id='hKfZ4'><dt id='hKfZ4'><q id='hKfZ4'><span id='hKfZ4'><b id='hKfZ4'><form id='hKfZ4'><ins id='hKfZ4'></ins><ul id='hKfZ4'></ul><sub id='hKfZ4'></sub></form><legend id='hKfZ4'></legend><bdo id='hKfZ4'><pre id='hKfZ4'><center id='hKfZ4'></center></pre></bdo></b><th id='hKfZ4'></th></span></q></dt></tr></i><div id='hKfZ4'><tfoot id='hKfZ4'></tfoot><dl id='hKfZ4'><fieldset id='hKfZ4'></fieldset></dl></div>
          <tfoot id='hKfZ4'></tfoot>
        • <legend id='hKfZ4'><style id='hKfZ4'><dir id='hKfZ4'><q id='hKfZ4'></q></dir></style></legend>

              • <bdo id='hKfZ4'></bdo><ul id='hKfZ4'></ul>

                  本文介绍了如何总结 C++ 向量的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  std::vector 中所有元素总和的方法是什么?

                  What are the good ways of finding the sum of all the elements in a std::vector?

                  假设我有一个向量 std::vector;向量,其中包含一些元素.现在我想找到所有元素的总和.相同的方法有哪些不同?

                  Suppose I have a vector std::vector<int> vector with a few elements in it. Now I want to find the sum of all the elements. What are the different ways for the same?

                  推荐答案

                  其实方法很多.

                  int sum_of_elems = 0;
                  

                  C++03

                  1. 经典 for 循环:

                  1. Classic for loop:

                   for(std::vector<int>::iterator it = vector.begin(); it != vector.end(); ++it)
                       sum_of_elems += *it;
                  

                • 使用标准算法:

                • Using a standard algorithm:

                   #include <numeric>
                  
                   sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);
                  

                  重要提示:最后一个参数的类型不仅用于初始值,还用于结果的类型.如果你在那里放一个 int,即使向量有浮点数,它也会累积整数.如果要对浮点数求和,请将 0 更改为 0.00.0f (感谢 nneonneo).另请参阅下面的 C++11 解决方案.

                  Important Note: The last argument's type is used not just for the initial value, but for the type of the result as well. If you put an int there, it will accumulate ints even if the vector has float. If you are summing floating-point numbers, change 0 to 0.0 or 0.0f (thanks to nneonneo). See also the C++11 solution below.

                  C++11 及更高版本

                  1. b.即使在未来发生变化的情况下,也会自动跟踪向量类型:

                  1. b. Automatically keeping track of the vector type even in case of future changes:

                   #include <numeric>
                  
                   sum_of_elems = std::accumulate(vector.begin(), vector.end(),
                                                  decltype(vector)::value_type(0));
                  

                • 使用 std::for_each:

                   std::for_each(vector.begin(), vector.end(), [&] (int n) {
                       sum_of_elems += n;
                   });
                  

                • 使用基于范围的 for 循环(感谢 Roger Pate):

                   for (auto& n : vector)
                       sum_of_elems += n;
                  

                • 这篇关于如何总结 C++ 向量的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:减少 stl 向量的容量 下一篇:如何创建向量向量的笛卡尔积?

                  相关文章

                  最新文章

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

                  1. <legend id='0V8hZ'><style id='0V8hZ'><dir id='0V8hZ'><q id='0V8hZ'></q></dir></style></legend>
                  2. <small id='0V8hZ'></small><noframes id='0V8hZ'>

                    <tfoot id='0V8hZ'></tfoot>

                    • <bdo id='0V8hZ'></bdo><ul id='0V8hZ'></ul>