- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个包含一些数据的类,我想添加 begin()
和 end()
提供数据 ID 迭代器的函数。
我正在使用 Boost counting_iterator
:
#include <iostream>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/iterator/counting_iterator.hpp>
template<class T>
class ContainerTpl {
public:
typedef std::size_t Id;
typedef boost::counting_iterator<Id> const_iterator;
ContainerTpl() {}
const_iterator begin() {
return boost::counting_iterator<Id>(0);
}
const_iterator end() {
return boost::counting_iterator<Id>(container_.size());
}
private:
std::vector<T> container_;
};
int main () {
typedef ContainerTpl<double> Container;
Container c;
BOOST_FOREACH (Container::Id cid, c) {
std::cerr << cid << std::endl;
}
return 0;
}
typedef
至
vector
将是不够的。我真的需要那个带有 ID 迭代器的类。
In file included from boost/foreach.hpp:71,
from a.cpp:3:
boost/mpl/eval_if.hpp: In instantiation of ‘boost::mpl::eval_if<mpl_::bool_<false>, boost::range_const_iterator<ContainerTpl<double> >, boost::range_mutable_iterator<ContainerTpl<double> > >’:
boost/foreach.hpp:355: instantiated from ‘boost::foreach_detail_::foreach_iterator<ContainerTpl<double>, mpl_::bool_<false> >’
a.cpp:25: instantiated from here
boost/mpl/eval_if.hpp:38: error: no type named ‘type’ in ‘struct boost::range_mutable_iterator<ContainerTpl<double> >’
a.cpp: In function ‘int main()’:
a.cpp:25: error: no matching function for call to ‘begin(const boost::foreach_detail_::auto_any_base&, boost::foreach_detail_::type2type<ContainerTpl<double>, mpl_::bool_<false> >*, boost::mpl::o\
r_<boost::mpl::and_<boost::mpl::not_<boost::is_array<ContainerTpl<double> > >, mpl_::bool_<false>, mpl_::bool_<true>, mpl_::bool_<true>, mpl_::bool_<true> >, boost::mpl::and_<boost::mpl::not_<boo\
st::foreach::is_noncopyable<ContainerTpl<double> > >, boost::foreach::is_lightweight_proxy<ContainerTpl<double> >, mpl_::bool_<true>, mpl_::bool_<true>, mpl_::bool_<true> >, mpl_::bool_<false>, m\
pl_::bool_<false>, mpl_::bool_<false> >*)’
a.cpp:25: error: no matching function for call to ‘end(const boost::foreach_detail_::auto_any_base&, boost::foreach_detail_::type2type<ContainerTpl<double>, mpl_::bool_<false> >*, boost::mpl::or_\
<boost::mpl::and_<boost::mpl::not_<boost::is_array<ContainerTpl<double> > >, mpl_::bool_<false>, mpl_::bool_<true>, mpl_::bool_<true>, mpl_::bool_<true> >, boost::mpl::and_<boost::mpl::not_<boost\
::foreach::is_noncopyable<ContainerTpl<double> > >, boost::foreach::is_lightweight_proxy<ContainerTpl<double> >, mpl_::bool_<true>, mpl_::bool_<true>, mpl_::bool_<true> >, mpl_::bool_<false>, mpl\
_::bool_<false>, mpl_::bool_<false> >*)’
a.cpp:25: error: no matching function for call to ‘deref(const boost::foreach_detail_::auto_any_base&, boost::foreach_detail_::type2type<ContainerTpl<double>, mpl_::bool_<false> >*)’
namespace boost
{
// specialize range_mutable_iterator and range_const_iterator in
// namespace boost
template<class T>
struct range_mutable_iterator< ContainerTpl<T> > {
typedef typename ContainerTpl<T>::const_iterator type;
};
template<class T>
struct range_const_iterator< ContainerTpl<T> > {
typedef typename ContainerTpl<T>::const_iterator type;
};
} // end namespace
最佳答案
Boost 文档中有一个关于此的页面:
http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/foreach/extensibility.html
总之你需要定义 boost::range_mutable_iterator<>
为您的类型,以便编译器可以实例化模板类型 BOOST_FOREACH
正在尝试使用。
为 future 的谷歌员工编辑:
我不确定那个“沙箱”URL 是否总是指向最新版本,或者是一个最终会中断的临时位置。这是当前版本的链接,它可能更稳定,但会过时:
http://www.boost.org/doc/libs/1_50_0/doc/html/foreach/extensibility.html
关于boost - 自定义迭代器不适用于 BOOST_FOREACH?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5863805/
我有一个包含一些数据的类,我想添加 begin()和 end()提供数据 ID 迭代器的函数。 我正在使用 Boost counting_iterator : #include #include
我正在编写 C++98(抱歉),但使用的是 C 库,它有许多对象存储在以下形式的数据结构中: struct c_container { size_t len; int data[1];
我尝试使用 Boost 库,我复制了整个 boost 文件夹,除了 docs、libs、more、status、tools 文件夹。 当我尝试使用下面的代码块时,我的编译器无法识别两件事。 vecto
我想使用 BOOST_FOREACH在我的代码库中使用我无法更改的遗留容器类型之一。 我在该类型上定义了以下方法: .length() 返回容器中元素的当前数量 .operator[](unsigne
我尝试在模板函数中使用 BOOST_FOREACH 和自定义但类型参数独立的迭代器。我收到 4-5 个错误,表明我的迭代器类没有很好地定义为迭代器。 在不使用 BOOST_FOREACH 的情况下重写
我想知道在调用之前获取对 vector 的引用是否有任何好处BOOST_FOREACH 或返回引用的方法调用是否会被自动使用?例如,以下两个循环中的哪一个将等同于第三个循环? vector& my_m
给定以下简单代码: list m_listOfOBjects; .... MyClass* ptrToMyClass; BOOST_FOREACH(MyClass object, m_listOfOb
我实现了一个简单的类 MyClass,其中有一个分配有 new 的数组(我知道我可以使用 STL 容器,但我试图理解他们是如何工作的)。我还创建了一个迭代器子类,能够迭代 MyClass 对象的所有元
我想使用 BOOST_FOREACH 宏来迭代我的 vector 中的一堆值。 vector 看起来像这样: struct _Element { int key; // more va
我测试了我的程序,并决定将 BOOST_FOREACH 宏更改为带有 const_iterator 的简单 for 循环。 我收到了意想不到的结果:使用 for 时程序运行速度变慢。 然后我写了一个小
我有一个 BOOST_FOREACH 循环来遍历列表。不幸的是,我还需要将迭代器缓存到特定项。 typedef List::iterator savedIterator; BOOST_FOREACH(
当使用BOOST_FOREACH时,下面的代码安全吗? BOOST_FOREACH (const std::string& str, getStrings()) { ... } ... std::
如果 BOOST_FOREACH 正在迭代的容器在 BOOST_FOREACH 范围内发生更改,会发生什么情况? BOOST_FOREACH 是否“卡住”初始状态? 最佳答案 在这种情况下,行为是未定
考虑以下代码,使用 BOOST_FOREACH 宏迭代一个侵入式列表: #include #include typedef boost::intrusive::list > MyList; voi
来自 boost doc , This results in near-optimal code generation; the performance of BOOST_FOREACH is usu
我有一个场景 vector ,vector .迭代元素的正确方法是什么,是否作为引用? 例如这个: BOOST_FOREACH(Scene scene, mScenes) { .....
我有以下宏: #define FOREACH(decl, c) BOOST_FOREACH(decl, std::make_pair((c).begin(), (c).end())) (我正在使用这个
我想听听您对 BOOST_FOREACH 使用的建议。 我已经阅读了它,因为它是一个非常沉重的标题,但实际上并不推荐它。 此外,它强制使用“break”和“continue”语句,因为您不能真正拥有由
我知道应该最后归咎于 boost 或编译器,但我在这里看不到其他解释。我正在使用 msvc 2008 SP1 和 boost 1.43。 在以下代码片段中,执行永远不会离开 第三个 BOOST_FOR
你可以使用 boost::filesystem 和 BOOST_FOREACH 遍历目录中的所有文件吗?我试过了 path dirPath = ... int fileCount = 0; BOOST
我是一名优秀的程序员,十分优秀!