C++ 类型的解引用迭代器

标签 c++ iterator

我试图创建一个函数来对 std::vector 的所有元素求和:

template<typename IteratorT>
auto sum(IteratorT first, IteratorT last) -> decltype(*first) {
    decltype(*first) sum = 0;
    for (; first != last; ++first)
        sum += *first;

    return sum;
}

我得到了这个错误:

cannot convert from 'int' to 'int&'

经过一些研究,我发现了这个:std::iterator_traits<IteratorT>::difference_type .将我的代码更改为:

template<typename IteratorT>
auto sum(IteratorT first, IteratorT last) -> typename std::iterator_traits<IteratorT>::difference_type {
    std::iterator_traits<IteratorT>::difference_type sum = 0;
    for (; first != last; ++first)
        sum += *first;

    return sum;
}

它确实有效,但我不确定为什么以及它是否是一个好的解决方案。最后我有两个问题:

1) 为什么 decltype(*first)返回 int&而不是 int正如我所料
2)到底是什么typename之前std::iterator_traits<IteratorT>::difference_type做和为什么sum如果我删除它,功能将不起作用

最佳答案

主要有两个问题:

  • 解引用迭代器的类型是引用,它可以是const,对于std::vector,它可能与 vector 的非常不同项目类型。

  • 当项目类型是例如bool,您不想在 bool 类型中求和。

下面的代码是一种解决方案:

#include <iterator>     // std::iterator_traits

template< class Iter >
auto sum( Iter first, Iter last )
    -> decltype( typename std::iterator_traits<Iter>::value_type() + 0 )
{
    decltype( typename std::iterator_traits<Iter>::value_type() + 0 ) sum = 0;
    for (; first != last; ++first)
        sum += *first;
    return sum;
}

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

#define ITEMS( x ) begin( x ), end( x )

auto main()
    -> int
{
    vector<double> const v1 = {3, 1, 4, 1, 5};
    cout << sum( ITEMS(v1) ) << endl;

    vector<bool> const v2 = {0, 1, 1, 0, 1};
    cout << sum( ITEMS( v2) ) << endl;
}

请注意,您不必定义自己的sum:有std::accumulate

关于C++ 类型的解引用迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38514449/

相关文章:

c++ - boost 单元测试 : BOOST_CHECK_CLOSE returns 1. #INF%

c++ - 指向派生类的基指针

c++ - 对于输入迭代器,为什么 a == b 并不意味着++a ==++b?

java - 按下一个键后从迭代器中删除对象

java - Univocity:使用 CsvRoutines 迭代 beans 时,为什么不能使用迭代器删除功能?

c++ - 为什么 std::ranges 算法缺少并行重载(采用执行策略参数)?

java - 哪种语言介于 C++ 和 Java 之间?

C++:使用 try/catch 访问未知大小的数组

c++ - 为什么我的 Rcpp 代码比 glmnet 慢得多?

java - 如何在我自己的集合上使用 For-Each 循环?