我有一个XML结构:
<root>
<SomeElement>
<AnotherElement>
<ElementIWant x="1" y="1"/>
</AnotherElement>
</SomeElement>
<SomeElement>
<AnotherElement>
<ElementIWant x="1" y="1"/>
<ElementIWant x="2" y="1"/>
<ElementIWant x="3" y="1"/>
</AnotherElement>
</SomeElement>
</root>
Run Code Online (Sandbox Code Playgroud)
这是被读入boost::property_tree,有1..Many <SomeElement>秒,然后在该单元内任意深度有可能是1..Many <ElementIWant>小号
有没有办法<ElementIWant>按照它们在doc中出现的顺序直接(在单个循环中)迭代?
我看过equal_range
void iterateOverPoints()
{
const char* test =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>"
"<SomeElement>"
"<AnotherElement>"
"<ElementIWant x=\"1\" y=\"1\"/>"
"</AnotherElement>"
"</SomeElement>"
"<SomeElement>"
"<AnotherElement>"
"<ElementIWant x=\"1\" y=\"1\"/>"
"<ElementIWant x=\"2\" y=\"1\"/>"
"<ElementIWant x=\"3\" y=\"1\"/>"
"</AnotherElement>"
"</SomeElement>"
"</root>";
boost::property_tree::ptree message;
std::istringstream toParse(test);
boost::property_tree::read_xml(toParse,result_tree);
//Now we need to locate the point elements …Run Code Online (Sandbox Code Playgroud) 我从我的代码中得到了"超出装饰名称长度"的警告我已经查看了类似问题的SO答案.*NB我知道如何关闭它(pragma),我也知道这是一个警告我可以"忽略"并且基于unix的编译器可能不会发出它:P*
这个问题有一个实际的元素,如果我遇到错误,我必须通过一些非常可怕的文本和模板.这些错误主要是由于当前的拼写错误,但如果我有任何微妙的内容,实际上找到问题是一场噩梦.
我的代码编译和工作,但正如我上面所说,我得到一个警告.我有一个"相对"的小表达
...
auto expression = (l,aComma,w,aComma,x,aComma,y,aComma,z);
std::cout << expression;
Run Code Online (Sandbox Code Playgroud)
我从我为表达式模板定义的逗号运算符生成(我可以发布,如果需要,但试图保持这个最小 - 漂亮标准的东西复制):
//
//Expression
//
template < class E >
struct Expr
{
E expr_;
Expr (E e) : expr_ (e) {}
std::ostream& print(std::ostream& out) { return expr_.print(out); }
};
template < class L, class H, class OP >
struct BinExpr
{
L l_;
H h_;
BinExpr (L l, H h) : l_ (l), h_ (h) {}
std::ostream& print(std::ostream& out) { l_.print(out) ; return h_.print(out); }
};
template< …Run Code Online (Sandbox Code Playgroud) 我有一个正在循环迭代的地图std::for_each。我使用嵌套绑定和简单的辅助函数来获取我需要的对的元素。这必须可以在 c++11 之前的代码中使用。
typedef std::map<int, Foo> MapType;
std::for_each(
testMap.begin() ,
testMap.end() ,
std::bind(
&Foo::myIncredibleFunction ,
std::bind(
&ValueExtractor< MapType::key_type , MapType::mapped_type >::getValue,
std::placeholders::_1 )));
Run Code Online (Sandbox Code Playgroud)
这很好用(尽管我确信可以改进)
我还将当前代码库从当前级别迁移到 C++11,因此我正在考虑是否可以使用该语言的新功能来改进代码(可读性、效率)。例如,我尝试了以下操作(未成功 - 请参阅下面的模板错误)
std::for_each(
testMap.begin() ,
testMap.end() ,
std::bind(
&Foo::myIncredibleFunction ,
std::get<1>( std::placeholders::_1 )));
Run Code Online (Sandbox Code Playgroud)
错误
1>d:\projects\test_bind\test_bind\test_bind.cpp(48): error C2780:
std::bind ...<Snip>...
: expects 6 arguments - 2 provided
Run Code Online (Sandbox Code Playgroud)
智能感知还具有以下功能:
IntelliSense: no instance of overloaded function "std::get" matches the argument list
argument types are: (std::_Ph<1>)
Run Code Online (Sandbox Code Playgroud)
我尝试了各种用法组合来std:get<1>()尝试替换我的内部绑定,但没有成功。我怀疑我没有正确理解这一点,但感觉我应该能够做我想做的事。有没有办法使用std::get调用而不使用辅助函数/函子来执行此操作?
编辑:我认为 KennyTM 已经就我在代码中实际执行的操作提出了我的答案,它比我的方法好得多。我仍然对 …
我正在编写一个需要拦截系统信号的多线程程序(例如SIGINT).我想知道是否有一种标准化的方式来"捕获"这些信号,如: