使用迭代器执行对象操作的函数

Function using iterators to perform a object operation

本文关键字:操作 函数 对象 执行 迭代器      更新时间:2023-10-16

假设我有一个std::array<SomeType, N>,我想调用一个函数,该函数使用迭代器来处理std::array中的对象,但不知道容器是std::array

SomeType 是一个具有公共成员函数 doSomething() 的类

例如,函数可能是:

template<typename Iterator>
void action(Iterator &beg, Iterator &end) {
  for (; beg != end; ++beg)
    beg->doSomething();
}

可以通过以下方式调用此函数:

int main() {
  std::array<SomeType, 10> a;
  action<std::array<SomeType, 10>::iterator>(a.begin(), a.end());
}

但我想知道这是否是这样做的方法?特别是因为模板可以用于每个类。有没有办法在不让函数知道容器是std::array的情况下将函数限制为SomeType

  1. 修复代码:不应要求左值参数。事实上,迭代器旨在有效地复制。

    template<typename Iterator>
    void action(Iterator beg, Iterator end)
    //          ^^^^^^^^^^^^  ^^^^^^^^^^^^
    
  2. 让模板参数推导完成它的工作:

    action(a.begin(), a.end());
    

请注意,标准库已经有许多算法涵盖了"在某个容器中的某个范围内做同样的事情"的一般情况:

#include <array>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
struct SomeType
{
  void doSomething();
  SomeType mutatedCopy() const;
  int someValue() const;
};
int add_value(int i, const SomeType& st) {
  return i + st.someValue();
}
void call_something(SomeType& st) { st.doSomething(); }
auto mutate_copy(SomeType const& st) { return st.mutatedCopy(); }
int main() {
  std::array<SomeType, 10> a;
  std::vector<SomeType> b;
  std::for_each(a.begin(), a.end(), call_something);
  std::for_each(b.begin(), b.end(), call_something);
  std::transform(a.begin(), a.end(), a.begin(), mutate_copy);
  std::transform(b.begin(), b.end(), b.begin(), mutate_copy);
  auto tot = std::accumulate(a.begin(), a.end(), 0, add_value)
           + std::accumulate(b.begin(), b.end(), 0, add_value);
  // you can even transform into dissimilar containers:
  std::transform(a.begin(), a.end(), std::back_inserter(b), mutate_copy);
}