📜  c++ 计算向量元素 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:52.778000             🧑  作者: Mango

代码示例1
// 1.
for(std::vector::iterator it = vector.begin(); it != vector.end(); ++it)
    sum_of_elems += *it;

// 2.
// #include 
sum_of_elems = std::accumulate(vector.begin(), vector.end(), 0);

// C++11 and higher
// 3.
// #include 
sum_of_elems = std::accumulate(vector.begin(), vector.end(),
                               decltype(vector)::value_type(0));

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