c++11中一个XMLElement类的基于Tricky-range的循环实现
Tricky range based for loop implementation for an XML Element class in c++11
如何让这样的东西同时用于元素和属性的基于范围的循环?
#include <list>
#include "XMLAttribute.h"
namespace XML
{
class Element
{
private:
typedef std::list<Attribute> attribute_container;
typedef std::list<Element> element_container;
public:
XMLElement();
bool has_attributes() const;
bool has_elements() const;
bool has_data() const;
const std::string &name() const;
const std::string &data() const;
private:
std::string _name;
std::string _data;
attribute_container _attributes;
element_container _elements;
};
}
我希望能够使用这样的东西:
for (XML::Element &el : element) { .. }
for (XML::Attribute &at : element) { .. }
并阻止类似for (auto &some_name : element) { .. } //XML::Element or XML::Attribute?
的内容。
这样实现它是个好主意吗?还是应该更改我的设计?
正确的答案是为Element节点提供返回子属性和元素范围的函数。因此,您可以这样做:
for(auto &element : element.child_elements()) {...}
for(auto &attribute : element.attributes()) {...}
child_elements
函数将返回某种存储两个迭代器的类型,比如boost::iterator_range。CCD_ 3将同样返回属性元素的范围。
相关文章:
- 如果没有malloc,链表实现将失败
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 我应该实现右值推送功能吗?我应该使用std::move吗
- 如何正确实现和访问运算符的各种自定义枚举器
- C++Union/Struct位域的实现和可移植性
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 在c++中实现LinkedList时,应出现未处理的错误
- 为左值和右值的包装器实现C++范围
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 使用GSoap实现ONVIF
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 用于AVX的ln(x)的实现,m256
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在C++中,如何在类和函数(可能是模板化的)的头中编写完整的实现
- c++11中一个XMLElement类的基于Tricky-range的循环实现