重构“哑巴”使用容器迭代器将函数转换为通用 STL 风格

发布于 2024-09-04 05:06:10 字数 979 浏览 14 评论 0原文

我已经设法理解了 C++ 的一些功能(for_each、映射函数、使用迭代器...),但是用于接收通用容器和迭代器的模板和函数参数列表的构造仍然让我困惑。我有一个实际的例子,希望有人能为我说明:

采用以下函数来处理传入的 std::vector 并构建进程的许多数据点/迭代的运行总数:

/* the for-loop method - not very savvy */
void UpdateRunningTotal (int_vec& total, int_vec& data_point) {
  for (int i = 0; i < V_SIZE; i++) {
    total[i] += data_point[i];
  }
}

typedef int_vec std::vector<int>;
int_vec running_total (V_SIZE, 0);  // create a container to hold all the "data points" over many iterations
/* further initialization, and some elaborate loop to create data points */

UpdateRunningTotal (running_total, iteration_data);
/* further processing */

上面的方法有效,但我' d 更愿意有一个接受迭代器并执行求和的函数。更好的是,有一个带有推导类型的通用参数列表,而不是指定容器类型,即:

UpdateRunningTotal (iteration_data.begin(), iteration_data.end(), running_total.begin());

我在这一点上真的迷失了,需要一些指导来找到如何定义模板和参数列表以使函数通用。模板和函数定义是什么样的?我已经熟悉使用 STL 功能执行此特定任务的方法 - 我正在寻找通用函数/模板定义的说明。

I've managed to wrap my head around some of C++'s functional capacities (for_each, mapping functions, using iterators...) but the construction of the templates and function argument lists for taking in generic containers and iterators still eludes me. I have a practical example I'm hoping someone can illustrate for me:

Take the following function that processes an incoming std::vector and builds a running total of many data-points/iterations of a process:

/* the for-loop method - not very savvy */
void UpdateRunningTotal (int_vec& total, int_vec& data_point) {
  for (int i = 0; i < V_SIZE; i++) {
    total[i] += data_point[i];
  }
}

typedef int_vec std::vector<int>;
int_vec running_total (V_SIZE, 0);  // create a container to hold all the "data points" over many iterations
/* further initialization, and some elaborate loop to create data points */

UpdateRunningTotal (running_total, iteration_data);
/* further processing */

The above works, but I'd much rather have a function that takes iterators and performs this summation. Even better, have a generic parameter list with the type deduced instead of specifying the container type, i.e.:

UpdateRunningTotal (iteration_data.begin(), iteration_data.end(), running_total.begin());

I'm really lost at this point and need a little guidance to find how to define the template and argument lists to make the function generic. What would the template and function definition look like? I'm already familiar with a way to perform this specific task using STL functionality - I'm looking for illustration of the generic function/template definition.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏见 2024-09-11 05:06:10

您可以使用 std::transformstd::plus

std::transform(iteration_data.begin(), iteration_data.end(),
                running_total.begin(), iteration_data.begin(), std::plus<int>());

在你的函数中,这将是:

template <typename Iter1, typename Iter2>
void UpdateRunningTotal(Iter1 pBegin, Iter1 pEnd, Iter2 pBegin2)
{
    typedef typename std::iterator_traits<Iter1>::value_type value_type;

    std::transform(pBegin, pEnd, pBegin2, pBegin, std::plus<value_type>());
}

You could use std::transform and std::plus:

std::transform(iteration_data.begin(), iteration_data.end(),
                running_total.begin(), iteration_data.begin(), std::plus<int>());

And in your function, that would be:

template <typename Iter1, typename Iter2>
void UpdateRunningTotal(Iter1 pBegin, Iter1 pEnd, Iter2 pBegin2)
{
    typedef typename std::iterator_traits<Iter1>::value_type value_type;

    std::transform(pBegin, pEnd, pBegin2, pBegin, std::plus<value_type>());
}
空心↖ 2024-09-11 05:06:10

好吧,我可以给你一个函数签名,你必须用正确的实现来填充它,因为你的规范现在对我来说没有意义。


template < typename InputIterator, typename OutputIterator >
?? UpdateRunningTotal(InputIterator beg, InputIterator end, OutputIterator dest)
{
}

Well, I could give you a function signature that you'll have to fill with correct implementation since your specification isn't making sense to me right now.


template < typename InputIterator, typename OutputIterator >
?? UpdateRunningTotal(InputIterator beg, InputIterator end, OutputIterator dest)
{
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文