c++ - 为 STL 容器传递模板化迭代器

标签 c++ templates stl iterator

对于我的 C++ 类的练习(尚未涵盖 Boost),我在编写模板化方法来接受两个迭代器以对 STL 容器中的数值求和时遇到问题。
考虑以下示例:

#include <iostream>
#include <iterator>
#include <vector>

template<typename T>
double Sum(const T & c) {
    return 42.0;    // implementation stubbed
}

// need help writing this method signature to accept two iterators
template<typename T>
double Sum(const typename T::const_iterator & begin,
           const typename T::const_iterator & end) {
    return 43.0;    // another implementation stub
}

int main() {
    std::vector<double> v;
    v.push_back(3.14);
    v.push_back(2.71);
    v.push_back(1.61);    // sums to 7.46

    std::cout << Sum(v) << ' '              // line 23
              << Sum(v.begin(), v.end())    // line 24
              << '\n';
}

我希望这段代码输出 42 43 , 但编译失败。
g++ 给我的错误是:

test_exercise2.cpp: In function ‘int main()’:
test_exercise2.cpp:24: error: no matching function for call to ‘Sum(__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >)’

如果我注释掉第 24 行,我会得到 42正如预期的那样作为输出。
无论是否存在第二个模板化方法,我都会收到相同的错误消息,因此出于某种原因,它无法将第 24 行上的调用解析为我编写的第二个方法。

接受两个迭代器的方法必须有什么签名?


我坚持这个的原因是因为我需要支持对 std::map<K, V> 的第二个元素求和.这将需要另外两个重载来调用 ->second而不是取消引用迭代器:
1. template<typename K, typename V> double Sum(const std::map<K, V> & m); (我对这个没意见)
2. 另一个涉及 map 上的迭代器。

我觉得我可以为 std::map 编写方法如果我能弄清楚如何为 std::list 指定迭代器的传递和 std::map .我接受使用模板模板的解决方案。


编辑:问题的精确措辞(省略非贡献句子)。
“上一个练习”中的容器是 std::vector<double> , std::list<double> , std::map<std::string, double> .

Create a template function called Sum() that accepts the template argument T as input and returns a double. The template argument will be a container.

  • In the implementation get an iterator (T::const_iterator) for the end. Then create a loop that iterates the container T and adds all values. Finally return the sum.
  • In the main program, call the Sum() function for the different container from the previous exercise.

The Sum() function created calculates the sum of the complete container. Also create a Sum() function that calculates the sum between two iterators. The function then uses the template argument for the iterator type and accepts two iterators, the start and end iterator.

最佳答案

你把这个复杂化了。你想要一对任何类型的迭代器?好吧,这就像..两个任何类型的参数一样简单。

template<typename Iterator>
double Sum(const Iterator& begin,
           const Iterator& end) {
    return 43.0;    // another implementation stub
}

问题已解决。

顺便说一下,从 C++ 标准库中得到一个提示:如果您不能取消对迭代器的引用,请让用户提供一个函数来从迭代器中获取值。不要特例 std::map 因为明天有 std::unordered_map 和后天 boost::multimap 等等的乐趣。如果我想让你对 std::map 中的 求和,而不是对值求和怎么办?

您的硬编码案例有点复杂。必须来自 std::map 的一对迭代器?如果没有明确的模板参数,甚至不确定是否可能。

template<typename K, typename V, typename Comp, typename Alloc>
double Sum(
    const std::map<K, V, Comp, Alloc>& map
) { ... }

请注意,我特别指出它必须是 std::map 实例化。这允许编译器推导出参数。从这里,您可以访问迭代器。

关于c++ - 为 STL 容器传递模板化迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12208618/

相关文章:

c++ - 如何更新某些子集的排名?

c++ - 通过调用C++函数设置loader组件

c++ - 需要一些清晰的模板实现代码

c++ - 为什么这个可变参数模板参数的替换失败了? (在固定参数之前打包)

c++ - std::normal_distribution 的类型取决于模板

C++ std::map::iterator 什么也没找到,但下标运算符返回对映射值的引用?

c++ - push_back(*obj) 正在调用析构函数并导致 SDL_BlitSurface 崩溃

c++ - 如何在C++中加载caffe模型进行预测

构造函数中的函数的 C++ 问题

c++ - 减少 C++ 中的模板化类参数