C++/Boost:编写更强大的 sscanf 替代品
我想用 C++ 编写一个函数来替换 C 的 sscanf,将匹配项分配给迭代器。
基本上,我想要这样的东西:
string s = "0.5 6 hello";
std::vector<boost::any> any_vector;
sscanv(s, "%f %i %s", any_vector);
cout << "float: " << any_cast<float>(any_vector[0]);
cout << "integer: " << any_cast<integer(any_vector[1]);
cout << "string: " << any_cast<string>(any_vector[2]);
确切的细节可能有所不同,但你明白了。有什么实施的想法吗?
到目前为止的选项以及到目前为止的问题:
- std::istringstream:没有用于匹配常量表达式的操纵器
- Boost.Regex:不确定这是否有效,而且看起来更多 Boost.Spirit比必要的复杂
- :不认为这适用于动态生成的格式字符串,而且它看起来也比必要的复杂
- sscanf:它可以工作,但是是非标准的,等等,并且使用它需要大量的开销,因为参数的数量是在编译时确定的
I want to write a function in C++ to replace C's sscanf that assigns the matches to iterator.
Basically, I want something like:
string s = "0.5 6 hello";
std::vector<boost::any> any_vector;
sscanv(s, "%f %i %s", any_vector);
cout << "float: " << any_cast<float>(any_vector[0]);
cout << "integer: " << any_cast<integer(any_vector[1]);
cout << "string: " << any_cast<string>(any_vector[2]);
The exact details may vary, but you get the idea. Any ideas for implementation?
Options so far along with problems so far:
- std::istringstream: there's no manipulator for matching constant expressions
- Boost.Regex: not sure if this will work and it seems much more complicated than necessary for this
- Boost.Spirit: don't think this will work for dynamically generated format strings and it also seems more complicated then necessary
- sscanf: it would work, but is non-standard, etc, and using it would require a lot of overhead since the number of arguments is determined at compile time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是怎么回事?
缺少一些转换说明符 - 但基本上它是有效的。
What's about that?
Some conversions specifiers are missing – but principally it works.
如果你的格式字符串是在编译时确定的,那么有一些 variadic-template printf 替换写入。反转它们应该可以很好地工作。
然后您可以使用 istream 的 >>用于读取的运算符或 c-stdlib 函数。
If your format string is determined at compile time, there are some variadic-template printf replacements written. Inverting those should work reasonably well.
You could then use istream's >> operator for reading, or the c-stdlib functions.