如何使用std :: partial_sum并输出到std :: map?

冲浪码

我需要仅具有c ++ 11的{{0,1},{1,2},{2,3},{3,4},{4,5}}的输出映射。有任何想法吗?

std::map<int, int> m, out;
for( auto i=0; i < 5; ++i ) 
    m[i] = 1;

std::partial_sum( m.begin(), m.end(), std::inserter( out, out.begin() ),
        []( const std::pair<int,int>& a, const std::pair<int,int>& b ) 
             { return std::pair<int,int>( a.first, a.second + b.second ); } 
);

这给出了编译错误:

/usr/include/c++/5/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U1 = int; _U2 = int; _T1 = const int; _T2 = int]’:
/usr/include/c++/5/bits/stl_numeric.h:295:12:   required from ‘_OutputIterator std::partial_sum(_InputIterator, _InputIterator, _OutputIterator, _BinaryOperation) [with _InputIterator = std::_Rb_tree_iterator<std::pair<const int, int> >; _OutputIterator = std::insert_iterator<std::map<int, int> >; _BinaryOperation = main()::<lambda(const std::pair<int, int>&, const std::pair<int, int>&)>]’
../src/test_cumsum.cpp:43:130:   required from here
/usr/include/c++/5/bits/stl_pair.h:188:10: error: assignment of read-only member ‘std::pair<const int, int>::first’
first = std::forward<_U1>(__p.first);
迈尔斯·布德奈克(Miles Budnek)

你不能 至少不直接。问题是,std::map<int, int>::iterator::value_typestd::pair<const int, int>,并且const防止该类型从被分配给的对象。

-

看看这个可能实现std::partial_sum

template<class InputIt, class OutputIt, class BinaryOperation>
constexpr // since C++20
OutputIt partial_sum(InputIt first, InputIt last, 
                     OutputIt d_first, BinaryOperation op)
{
    if (first == last) return d_first;
 
    typename std::iterator_traits<InputIt>::value_type sum = *first;
    *d_first = sum;
 
    while (++first != last) {
       sum = op(std::move(sum), *first); // std::move since C++20
       *++d_first = sum;
    }
    return ++d_first;
}

请注意,sum每次迭代都会通过分配的结果进行修改op因为sum.firstconst这是不可能的; 因此编译错误。

-

您可以做的是定义一个包装std::map::iterator并剥离出的迭代器类型const例如,以下将起作用:

template <typename Pair>
struct RemoveFirstConstHelper
{
    using type = Pair;
};

template <typename T1, typename T2>
struct RemoveFirstConstHelper<std::pair<const T1, T2>>
{
    using type = std::pair<T1, T2>;
};

template <typename MapIterator>
class RemoveFirstConstIter
{
public:
    using difference_type = std::ptrdiff_t;
    using value_type = typename RemoveFirstConstHelper<typename MapIterator::value_type>::type;
    using pointer = value_type*;
    using reference = value_type;
    using iterator_category = std::input_iterator_tag;
    
    RemoveFirstConstIter(MapIterator it) : it_{it} {}
    
    reference operator*()
    {
        return *it_;
    }
    
    RemoveFirstConstIter& operator++()
    {
        ++it_;
        return *this;
    }
    
    RemoveFirstConstIter operator++(int) const
    {
        RemoveFirstConstIter temp{*this};
        ++temp;
        return temp;
    }
    
    bool operator==(const RemoveFirstConstIter& other) const
    {
        return it_ == other.it_;
    }
    
    bool operator!=(const RemoveFirstConstIter& other) const
    {
        return !(*this == other);
    }
    
private:
    MapIterator it_;
};

现场演示

或者,您可以只编写自己partial_sum的地图实现。对我来说,这似乎更简单。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

std :: partial_sum和Kahan算法

来自分类Dev

如何使用随机值创建std :: map

来自分类Dev

std :: partial_sum和std :: inclusive_scan有什么区别?

来自分类Dev

std :: partial_sum和std :: inclusive_scan有什么区别?

来自分类Dev

如何将std :: bind与std :: function和std :: map结合使用

来自分类Dev

如何使用boost :: mutex作为std :: map中的映射类型?

来自分类Dev

如何在带有boost :: phoenix的std :: map中使用?

来自分类Dev

如何最好地将emplace与std :: map结合使用

来自分类Dev

如何在带有boost :: phoenix的std :: map中使用?

来自分类Dev

如何实现std :: map的排序?

来自分类Dev

如何实现std :: map的排序?

来自分类Dev

如何返回std :: map项

来自分类Dev

C ++在std :: map <>中使用std :: set <>

来自分类Dev

在std :: map中使用std :: function

来自分类Dev

C ++在std :: map <>中使用std :: set <>

来自分类Dev

在std :: map中使用std :: function

来自分类Dev

在std :: map中使用'auto'

来自分类Dev

在std :: map中使用decltype

来自分类Dev

使用std :: map的Miniheap Lambda

来自分类Dev

使用std :: map :: at时出错

来自分类Dev

如何在值为std :: set的std :: map(从某物到集合的映射)中使用emplace()?

来自分类Dev

如何使用提取将不可复制的元素从std :: set移动到std :: map?

来自分类Dev

如何使用单个for循环遍历std :: map <string,int>和std :: vector <int>?

来自分类Dev

C ++:将自定义哈希键值从unordered_map输出到std :: cout时出错

来自分类Dev

如何使用以下模板为地图声明迭代器 - std::map<std::string, T> my_map?

来自分类Dev

如何在搜索中不使用std :: map的自定义比较功能(map :: find)?

来自分类Dev

如何在搜索中不使用std :: map的自定义比较功能(map :: find)?

来自分类Dev

如何获取std :: map中的键数?

来自分类Dev

如何修改从std :: map提取的节点密钥

Related 相关文章

  1. 1

    std :: partial_sum和Kahan算法

  2. 2

    如何使用随机值创建std :: map

  3. 3

    std :: partial_sum和std :: inclusive_scan有什么区别?

  4. 4

    std :: partial_sum和std :: inclusive_scan有什么区别?

  5. 5

    如何将std :: bind与std :: function和std :: map结合使用

  6. 6

    如何使用boost :: mutex作为std :: map中的映射类型?

  7. 7

    如何在带有boost :: phoenix的std :: map中使用?

  8. 8

    如何最好地将emplace与std :: map结合使用

  9. 9

    如何在带有boost :: phoenix的std :: map中使用?

  10. 10

    如何实现std :: map的排序?

  11. 11

    如何实现std :: map的排序?

  12. 12

    如何返回std :: map项

  13. 13

    C ++在std :: map <>中使用std :: set <>

  14. 14

    在std :: map中使用std :: function

  15. 15

    C ++在std :: map <>中使用std :: set <>

  16. 16

    在std :: map中使用std :: function

  17. 17

    在std :: map中使用'auto'

  18. 18

    在std :: map中使用decltype

  19. 19

    使用std :: map的Miniheap Lambda

  20. 20

    使用std :: map :: at时出错

  21. 21

    如何在值为std :: set的std :: map(从某物到集合的映射)中使用emplace()?

  22. 22

    如何使用提取将不可复制的元素从std :: set移动到std :: map?

  23. 23

    如何使用单个for循环遍历std :: map <string,int>和std :: vector <int>?

  24. 24

    C ++:将自定义哈希键值从unordered_map输出到std :: cout时出错

  25. 25

    如何使用以下模板为地图声明迭代器 - std::map<std::string, T> my_map?

  26. 26

    如何在搜索中不使用std :: map的自定义比较功能(map :: find)?

  27. 27

    如何在搜索中不使用std :: map的自定义比较功能(map :: find)?

  28. 28

    如何获取std :: map中的键数?

  29. 29

    如何修改从std :: map提取的节点密钥

热门标签

归档