我想解析一个字符串"{{0, 1}, {2, 3}}"
into a std::map
。我可以编写一个小函数来解析字符串<regex>
库,但我不知道如何检查给定的字符串是否采用有效的格式。如何验证字符串的格式?
#include <list>
#include <map>
#include <regex>
#include <iostream>
void f(const std::string& s) {
std::map<int, int> m;
std::regex p {"[\\[\\{\\(](\\d+),\\s*(\\d+)[\\)\\}\\]]"};
auto begin = std::sregex_iterator(s.begin(), s.end(), p);
auto end = std::sregex_iterator();
for (auto x = begin; x != end; ++x) {
std::cout << x->str() << '\n';
m[std::stoi(x->str(1))] = std::stoi(x->str(2));
}
std::cout << m.size() << '\n';
}
int main() {
std::list<std::string> l {
"{{0, 1}, (2, 3)}",
"{{4, 5, {6, 7}}" // Ill-formed, so need to throw an excpetion.
};
for (auto x : l) {
f(x);
}
}
注意:我觉得没有义务使用regex
来解决这个问题。任何类型的解决方案,包括通过减去子字符串来立即验证和插入的一些方法,都将受到赞赏。
在我看来,基于 Spirit 的解析器总是更加健壮和可读。用 Spirit 来解析也更有趣:-)。因此,除了 @Aleph0 的答案之外,我还想提供一个基于 Spirit-X3 的紧凑型解决方案 https://wandbox.org/permlink/cQRCb2R0xdJthGbS:
#include <string>
#include <map>
#include <iostream>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/home/x3.hpp>
int main() {
std::string input ="{{0, 1}, {2, 3}}";
using namespace boost::spirit::x3;
const auto pair = '{' > int_ > ',' > int_ > '}';
const auto pairs = '{' > (pair % ',') > '}';
std::map<int, int> output;
// ignore spaces, tabs, newlines
phrase_parse(input.begin(), input.end(), pairs, space, output);
for (const auto [key, value] : output) {
std::cout << key << ":" << value << std::endl;
}
}
请注意,我使用了运算符>
,意思是“期望”。因此,如果输入与预期不符,Spirit 就会抛出异常。如果您希望静默失败,请使用运算符>>
反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)