我需要将double值格式化为具有非常特定格式的坐标字符串,"DDMMSS.SSX"其中:
字段需要用零填充.空间不能被接受.格式化示例如下:
47.2535 ==> "471512.45N"
-0.123345 ==> "000724.04S"
Run Code Online (Sandbox Code Playgroud)
我已设法创建以下程序来完成这项工作.不过我有一些问题:
locls规则有更优雅的方式吗?它的目的是将绝对值存储到局部变量中value.是否有(希望更优雅)的方式来访问该fabs()功能?_1(_1 = _val等)的赋值是不必要的,因为我在局部变量中有值value.但是,如果我删除这些作业,我得到的只是"000000.00N".value.有更好的方法吗?我很乐意收到一些反馈意见
#include <boost/spirit/include/karma.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/bind.hpp>
namespace karma = boost::spirit::karma;
typedef std::back_insert_iterator<std::string> iterator_type;
struct genLongitude : karma::grammar<iterator_type, double()>
{
genLongitude()
: genLongitude::base_type(start)
{
using karma::eps;
using karma::int_;
using karma::char_;
using karma::_1;
using karma::_val;
using karma::right_align;
using boost::phoenix::static_cast_;
using …Run Code Online (Sandbox Code Playgroud) 我希望有人可以通过我在精神解析中使用>和>>操作符的无知来发光.
我有一个工作语法,顶级规则看起来像
test = identifier >> operationRule >> repeat(1,3)[any_string] >> arrow >> any_string >> conditionRule;
Run Code Online (Sandbox Code Playgroud)
它依赖于属性来自动将解析后的值分配给融合自适应结构(即boost元组).
但是,我知道一旦我们匹配operationRule,我们必须继续或失败(即我们不希望允许回溯尝试以其他规则开头identifier).
test = identifier >>
operationRule > repeat(1,3)[any_string] > arrow > any_string > conditionRule;
Run Code Online (Sandbox Code Playgroud)
这会导致一个神秘的编译器错误('boost::Container' : use of class template requires template argument list).Futz一点点以下编译:
test = identifier >>
(operationRule > repeat(1,3)[any_string] > arrow > any_string > conditionRule);
Run Code Online (Sandbox Code Playgroud)
但属性设置不再有效 - 我的数据结构在解析后包含垃圾.这可以通过添加类似的操作来修复[at_c<0>(_val) = _1],但这似乎有点笨拙 - 以及根据boost文档使事情变慢.
所以,我的问题是
()operationRule匹配之后是否真的停止了回溯(我怀疑不是,似乎如果(...)允许整个解析器内部失败回溯)?