集合中不带 for 循环的 n 个连续项的总和
Sum of n consecutive items in a collection without for loops
我正在寻找如何删除我的for循环以计算集合中n个连续项目的总和。
示例:对于集合 {1,2,1,3,2} 和 n=3(其中 n 是 要处理的连续项目数),结果将是 {0,0,4,6,6}
{1,2,1,3,2} 和 n=3 产生 {0,0,4,6,6}
要确定两个连续项目的总和,我知道我可以这样做:
std::adjacent_difference(std::begin(collection),
std::end(collection),
std::begin(adjacent_sum),
[](const int x, const int y) { return std::abs(x - y); });
但是要确定 n 个连续项目的总和,我坚持使用像 std::transform 这样的算法在我需要索引时处理值。我是否应该在算法之外使用 index 作为变量来维护状态。
下面是一个使用 Visual Studio 2019 编译的完整示例:
#include <iostream>
#include <numeric>
#include <vector>
// Compute the sum of the n consecutive items
// Ex: {1,2,1,3,2} and n=3
// result: {0,0,4,6,6}
std::vector<int> adjacent_difference_n(const std::vector<int> collection, const size_t num_consecutive_items)
{
std::vector<int> result {};
const auto collection_size = collection.size();
for (size_t idx = 0; idx < num_consecutive_items - 1; idx++)
{
result.emplace_back(0);
}
if (collection_size >= num_consecutive_items)
{
// For each element starting at num_consecutive_items
for (auto idx = num_consecutive_items - 1; idx < collection_size; idx++)
{
// Compute the sum of the previous num_consecutive_item
// Loop includes the current item
auto sum = 0;
auto prev_idx = idx - (num_consecutive_items - 1);
while (prev_idx <= idx)
{
sum += collection[prev_idx++];
}
result.emplace_back(sum);
}
}
return result;
}
int main()
{
const std::vector<int> collection = { 1, 2, 1, 3, 2 };
const auto result = adjacent_difference_n(collection, 3);
for (auto& value : result) std::cout << value << " ";
std::cout << std::endl;
return 0;
}
您可以通过减去第一项并添加新项来避免内部循环更新部分总和:
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
template <class T>
auto partial_sum_n(std::vector<T> const& src, size_t m)
{
// Initialize the resulting vector with zeroes
std::vector<T> result(src.size());
if (m == 0 or src.size() < m)
return result;
auto first = src.cbegin();
auto it = std::next(first, m);
// Sum the first elements and assign to the first non zero element
auto sum = std::accumulate(first, it, T(0));
auto dest = std::next(result.begin(), m - 1);
*dest = sum;
// Fill the rest of the vector
std::transform(it, src.cend(),
++dest,
[first, sum] (T const& x) mutable {
sum += x - *(first++); // <-- Just update the sum
return sum;
});
return result;
}
int main()
{
const std::vector<int> collection{ 1, 2, 1, 3, 2 };
const auto result = partial_sum_n(collection, 3);
for (auto const value : result)
std::cout << ' ' << value;
std::cout << 'n';
}
我终于找到了解决问题的方法。
对于任何感兴趣的人,我使用std::fill
和std::for_each
:
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
// Compute the sum of the n consecutive items
// Ex: {1,2,1,3,2} and n=3
// result: {0,0,4,6,6}
std::vector<int> adjacent_sum_n(const std::vector<int> collection, const size_t num_consecutive_items)
{
// Create the result collection with the same size than the specified collection
const auto collection_size = collection.size();
std::vector<int> result{};
result.resize(collection_size);
// Fill to zero the items that have not enough previous items
std::fill(begin(result), begin(result) + num_consecutive_items - 1, 0);
// For each remaining items, compute the sum the n consecutive items
auto idx = num_consecutive_items - 1;
std::for_each(begin(result) + idx,
end(result),
[&](auto& value)
{
const auto first_item_idx = idx - num_consecutive_items + 1;
const auto sum = std::accumulate(begin(collection) + first_item_idx,
begin(collection) + idx + 1,
0);
++idx;
value = sum;
});
return result;
}
int main()
{
const std::vector<int> collection = { 1, 2, 1, 3, 2 };
const auto result = adjacent_sum_n(collection, 3);
for (auto& value : result) std::cout << value << " ";
std::cout << std::endl;
return 0;
}
相关文章:
- 处理多个异常集合的C++方法
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 模板元程序查找相似的连续类型名称
- 如何将ampl中的集合表示为c++中的向量
- 检查值是否在集合p1和p2中,但不在p3中
- 带过滤器的现代迭代c++集合
- 如何将结构插入到集合中并打印集合的成员
- 集合上的输出迭代器:assign和increment迭代器
- 如何将函数集合传递给客户端类,以便将它们当作客户端类本身的成员使用
- 实现一个在集合上迭代的模板函数
- 尝试使用集合函数时出现分段错误
- 互斥指针的集合
- c++找不到具有相同哈希的无序集合元素
- EASTL矢量<向量<int>>连续的
- python集合的C++等价物是什么.计数器
- 当需要超过16GB的连续内存时,内存分配失败
- 如何在C++中递归地按相反顺序打印集合
- 我应该如何表示我拥有的连续元素序列?
- 集合中不带 for 循环的 n 个连续项的总和
- 在块集合中统一连续块的算法