首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >基于C++非迭代器的范围库?

基于C++非迭代器的范围库?
EN

Stack Overflow用户
提问于 2010-12-30 06:57:45
回答 3查看 1.3K关注 0票数 18

我一直对stl迭代器的不便感到沮丧,我正在寻找更有用的东西。特别是,一个更容易映射和过滤的概念,也是一个更容易实现的概念:基本上是C#/python/ruby/everything-but-C++风格的枚举。

我偶然看到Andrei Alexandrescu在2009年的 boostcon主题演讲中,他描述了一个范围概念,这正是我正在寻找的,甚至更多。

有没有人知道这样的事情是否真的实现了?我知道boost::range,但这不是一个理想的解决方案;它是用迭代器实现的(迭代器更令人困惑、更复杂、效率更低、通用性更差,并且使编写自己的迭代器至少与实现迭代器一样混乱)。不过,这总比什么都没有好。外面还有更好的东西吗?

编辑:有很多关于为什么这很有吸引力的讨论。更清楚地解释了动机。我意识到了与D的联系--但这不应该分散对论点本身的注意力。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-07-28 17:48:47

已经有几年了,但是看起来现在有一个库可以实现正确的函数样式序列:。

来自项目主页的代码示例:

对前10个正方形求和:

int total = stream::MakeStream::counter(1)
    .map([] (int x) { return x * x; })
    .limit(10)
    .sum();
票数 1
EN

Stack Overflow用户

发布于 2010-12-30 21:41:41

看起来自己做起来很容易--假设您不介意一些工作/键入

我还没有编译这篇文章,但是像这样的东西应该会让你前进一点。

template< typename T>
class enumeration : boost::noncopyable {
virtual T& next() = 0;
virtual bool has_next() = 0;
};

template< typename T>
class editable_enumeration : public enumeration<T> {
virtual void remove() = 0;
}

//That was simple enough, now for adapting the std containers we
//will use the iterator interface already exposed. For new classes,
//we can implement iterators in any way we want. (e.g. copy python or java)

template < class C >
class const_std_enumeration : public enumeration<C::value_type>
{
protected:
C::const_iterator iter_;
C::const_iterator end_;

public:
typedef C::value_type value_type;

const_std_enumeration( C const& c) :
iter_(c.begin()), end_(c.end()) { } //c++0x use std::begin(c), std::end(c) instead

virtual value_type& next() { if(iter_!=end_) return *iter_++; throw std::runtime_error("No more elements"); }
virtual bool has_next() { return iter_!=end_; }
};

template < class C>
class std_enumeration : public enumeration<C::value_type>
{
protected:
C& c_;
C::iterator iter_;
C::iterator end_;

public:
typedef C::value_type value_type;

std_enumeration( C& c) :
c_(c), iter_(vector.begin()), end_(vector.end()) { }

virtual value_type& next() { if(v_!=end_) return *iter_++; throw std::runtime_error("No more elements"); }
virtual bool has_next() { return iter_!=end_; }
virtual remove() { iter_ = c_.erase(iter_); }
};


//Since we can't add methods to std containers, we will use an
//overloaded free-function `enumeration` to get enumerations from ANY container
//We could use `auto_ptr` or `unique_ptr`, but for simplicity's sake, I'm
//just using raw pointers

template < class C >
editable_enumeration<C::value_type>* enumeration( C&c ) { return new std_enumeration<C>(c); }

template < class C >
enumeration<C::value_type>* enumeration( C const& c ) { return new const_std_enumeration<C>(c); }

对于所有其他容器,只需确保定义了enumeration并返回枚举或editable_enumeration。如果您的其他容器已经实现了迭代器接口,那么这应该是可行的

我们现在可以这样写:

template<typename T>
bool contains( enumeration<T>* e, T const& t) {
while(e->has_next())
  if ( t == e->next() )
    return true;
return false;
}
...
std::vector<int> v = getIntVector();
if( contains( enumeration(v), 10 ) ) std::cout<<"Enumeration contains 10\n";
std::list<int> l = getIntList();
if( contains( enumeration(l), 10 ) ) std::cout<<"Enumeration contains 10\n";

应该注意的是,迭代器概念相对于迭代器概念的一个巨大优势是,当has_next()返回false时,请求下一个元素。对于迭代器,++()是未定义的行为。对于枚举,它被定义为throw std::runtime_error(...),这对某些人来说可能更糟糕。

票数 5
EN

Stack Overflow用户

发布于 2011-09-01 01:35:01

也许这个会对你有所帮助:http://rangelib.synesis.com.au/

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4558606

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档