C ++系列/的xrange相当于STL或升压? [英] C++ range/xrange equivalent in STL or boost?

查看:150
本文介绍了C ++系列/的xrange相当于STL或升压?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有C ++相当于蟒蛇XRANGE发电机在任何STL或升压?

Is there C++ equivalent for python Xrange generator in either STL or boost?

基本上的xrange产生递增的编号,每个呼叫++运算符。
构造是这样的:

xrange basically generates incremented number with each call to ++ operator. the constructor is like this:

xrange(first, last, increment)

是希望做这样的事情使用boost每个:

was hoping to do something like this using boost for each:

foreach(int i, xrange(N))

我。我的for循环知道的。在我看来,他们是太多了样板。

I. am aware of the for loop. in my opinion they are too much boilerplate.

感谢

我为希望这样做的主要原因是因为我使用语音到文本的软件和编程循环通常的方式是困难的,即使使用code完成。它是更有效的有发音的构建

my main reason for wanting to do so is because i use speech to text software, and programming loop usual way is difficult, even if using code completion. It is much more efficient to have pronounceable constructs.

许多循环从零开始和增量接一个,这是默认的范围。我发现蟒蛇结构更直观<​​/ P>

many loops start with zero and increment by one, which is default for range. I find python construct more intuitive

 for(int i = 0; i < N; ++i)
 foreach(int i, range(N))

这就需要采取一系列功能参数:

functions which need to take range as argument:

 Function(int start, int and, int inc);
 function(xrange r);

我理解语言之间的差异,但是,如果在python特定的结构是对我来说非常有用,可以在C ++高效地实现,我没有看到一个理由不使用它。对于每一个结构是国外C ++以及但是人们使用它。

I understand differences between languages, however if a particular construct in python is very useful for me and can be implemented efficiently in C++, I do not see a reason not to use it. For each construct is foreign to C++ as well however people use it.

我把我在页面底部的执行情况以及本例使用。

I put my implementation at the bottom of the page as well the example usage.

在我的领域我和多维数组工作,经常排4张量。所以我会经常结束了4不同范围/增量嵌套的循环来计算规范化,索引等这些不一定是性能的循环,我更关心的正确性,可读性和修改能力。

in my domain i work with multidimensional arrays, often rank 4 tensor. so I would often end up with 4 nested loops with different ranges/increments to compute normalization, indexes, etc. those are not necessarily performance loops, and I am more concerned with correctness readability and ability to modify.

例如:

int function(int ifirst, int ilast, int jfirst, int jlast, ...);
versus
int function(range irange, range jrange, ...);

在上面的,如果需要不同的strids,你必须通过更多的变数,修改循环等最终你最终与整数/几乎完全相同环的质量。

In the above, if different strids are needed, you have to pass more variables, modify loops, etc. eventually you end up with a mass of integers/nearly identical loops.

的foreach和范围恰好解决了我的问题。熟悉平均C ++程序员不高我的关注名单上 - 问题领域是一个相当模糊的,有很多的元编程,上证所内在的,产生code

foreach and range solve my problem exactly. familiarity to average C++ programmer is not high on my list of concerns - problem domain is a rather obscure, there is a lot of meta-programming, SSE intrinsic, generated code.

推荐答案

升压有<一个href=\"http://www.boost.org/doc/libs/1%5F41%5F0/libs/iterator/doc/counting%5Fiterator.html\">counting_iterator据我所知,这似乎只允许在1步骤,全面的xrange功能递增,您可能需要自己实现一个类似的迭代器。

Boost has counting_iterator as far as I know, which seems to allow only incrementing in steps of 1. For full xrange functionality you might need to implement a similar iterator yourself.

总而言之,这可能看起来像这样(编辑:增加了一个迭代器的xrange第三超载,玩弄提升的迭代器门面):

All in all it could look like this (edit: added an iterator for the third overload of xrange, to play around with boost's iterator facade):

#include <iostream>
#include <boost/iterator/counting_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/foreach.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include <cassert>

template <class T>
boost::iterator_range<boost::counting_iterator<T> > xrange(T to)
{
    //these assertions are somewhat problematic:
    //might produce warnings, if T is unsigned
    assert(T() <= to);
    return boost::make_iterator_range(boost::counting_iterator<T>(0), boost::counting_iterator<T>(to));
}

template <class T>
boost::iterator_range<boost::counting_iterator<T> > xrange(T from, T to)
{
    assert(from <= to);
    return boost::make_iterator_range(boost::counting_iterator<T>(from), boost::counting_iterator<T>(to));
}

//iterator that can do increments in steps (positive and negative)
template <class T>
class xrange_iterator:
    public boost::iterator_facade<xrange_iterator<T>, const T, std::forward_iterator_tag>
{
    T value, incr;
public:
    xrange_iterator(T value, T incr = T()): value(value), incr(incr) {}
private:
    friend class boost::iterator_core_access;
    void increment() { value += incr; }
    bool equal(const xrange_iterator& other) const
    {
        //this is probably somewhat problematic, assuming that the "end iterator"
        //is always the right-hand value?
        return (incr >= 0 && value >= other.value) || (incr < 0 && value <= other.value);
    }
    const T& dereference() const { return value; }
};

template <class T>
boost::iterator_range<xrange_iterator<T> > xrange(T from, T to, T increment)
{
    assert((increment >= T() && from <= to) || (increment < T() && from >= to));
    return boost::make_iterator_range(xrange_iterator<T>(from, increment), xrange_iterator<T>(to));
}

int main()
{
    BOOST_FOREACH(int i, xrange(10)) {
        std::cout << i << ' ';
    }
    BOOST_FOREACH(int i, xrange(10, 20)) {
        std::cout << i << ' ';
    }
    std::cout << '\n';
    BOOST_FOREACH(int i, xrange(0, 46, 5)) {
        std::cout << i << ' ';
    }
    BOOST_FOREACH(int i, xrange(10, 0, -1)) {
        std::cout << i << ' ';
    }
}

至于别人说,我没有看到这个你买多过正常的循环。

As others are saying, I don't see this buying you much over a normal for loop.

这篇关于C ++系列/的xrange相当于STL或升压?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆