在C++中给定一个字符串,其中包含类型的范围和单个数字:
"2,3,4,7-9"我想将它解析为表单的向量:
2,3,4,7,8,9如果数字是由一个-分隔的,那么我想把所有的数字都推到这个范围内。否则我想推一个号码。
我试过使用这段代码:
const char *NumX = "2,3,4-7";
std::vector<int> inputs;
std::istringstream in( NumX );
std::copy( std::istream_iterator<int>( in ), std::istream_iterator<int>(),
std::back_inserter( inputs ) );问题是它不适用于范围。它只使用字符串中的数字,而不是范围内的所有数字。
发布于 2020-08-17 08:32:14
除了@J.Schultke的优秀示例之外,我建议以以下方式使用regexes:
#include <algorithm>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
void process(std::string str, std::vector<int>& num_vec) {
str.erase(--str.end());
for (int i = str.front() - '0'; i <= str.back() - '0'; i++) {
num_vec.push_back(i);
}
}
int main() {
std::string str("1,2,3,5-6,7,8");
str += "#";
std::regex vec_of_blocks(".*?\,|.*?\#");
auto blocks_begin = std::sregex_iterator(str.begin(), str.end(), vec_of_blocks);
auto blocks_end = std::sregex_iterator();
std::vector<int> vec_of_numbers;
for (std::sregex_iterator regex_it = blocks_begin; regex_it != blocks_end; regex_it++) {
std::smatch match = *regex_it;
std::string block = match.str();
if (std::find(block.begin(), block.end(), '-') != block.end()) {
process(block, vec_of_numbers);
}
else {
vec_of_numbers.push_back(std::atoi(block.c_str()));
}
}
return 0;
}当然,您仍然需要一点验证,然而,这将使您开始。
发布于 2020-08-17 07:54:55
您的问题包括两个独立的问题:
解析每个字符串,
时,
如果您首先在逗号处拆分整个字符串,则不必担心同时在连字符处拆分它。这就是你所说的分而治之的方法。
在,上分裂
This question应该告诉您如何在逗号处拆分字符串。
解析并添加到std::vector<int>中
在逗号处拆分字符串后,只需通过为每个字符串调用此函数将范围转换为单个数字:
#include <vector>
#include <string>
void push_range_or_number(const std::string &str, std::vector<int> &out) {
size_t hyphen_index;
// stoi will store the index of the first non-digit in hyphen_index.
int first = std::stoi(str, &hyphen_index);
out.push_back(first);
// If the hyphen_index is the equal to the length of the string,
// there is no other number.
// Otherwise, we parse the second number here:
if (hyphen_index != str.size()) {
int second = std::stoi(str.substr(hyphen_index + 1), &hyphen_index);
for (int i = first + 1; i <= second; ++i) {
out.push_back(i);
}
}
}注意,在连字符处分裂要简单得多,因为我们知道字符串中最多可以有一个连字符。在这种情况下,std::string::substr是最简单的方法。请注意,如果整数太大,无法适应int,则int会引发异常。
发布于 2020-08-17 15:03:09
到目前为止都是很好的解决方案。使用现代的C++和regex,您只需很少行代码就可以完成一个完整的解决方案。
多么?首先,我们定义一个与整数或整数范围匹配的正则表达式。它会看起来像这样
((\d+)-(\d+))|(\d+)真的很简单。首先是靶场。所以,一些数字,后面跟着一个连字符,还有一些数字。然后是普通整数:一些数字。所有的数字都分组。(牙套)。连字符不在匹配组中。
这一切都很容易,不需要进一步解释。
然后我们在循环中调用std::regex_search,直到找到所有匹配。
对于每一场比赛,我们检查,是否有子匹配,意味着一个范围。如果我们有子匹配,一个范围,那么我们将子匹配(包括)之间的值添加到结果的std::vector中。
如果我们只有一个普通整数,那么我们只添加这个值。
所有这些都给出了一个非常简单易懂的程序:
#include <iostream>
#include <string>
#include <vector>
#include <regex>
const std::string test{ "2,3,4,7-9" };
const std::regex re{ R"(((\d+)-(\d+))|(\d+))" };
std::smatch sm{};
int main() {
// Here we will store the resulting data
std::vector<int> data{};
// Search all occureences of integers OR ranges
for (std::string s{ test }; std::regex_search(s, sm, re); s = sm.suffix()) {
// We found something. Was it a range?
if (sm[1].str().length())
// Yes, range, add all values within to the vector
for (int i{ std::stoi(sm[2]) }; i <= std::stoi(sm[3]); ++i) data.push_back(i);
else
// No, no range, just a plain integer value. Add it to the vector
data.push_back(std::stoi(sm[0]));
}
// Show result
for (const int i : data) std::cout << i << '\n';
return 0;
}如果你还有更多的问题,我很乐意回答。
语言:用19社区版编译和测试C++ 17
https://stackoverflow.com/questions/63446295
复制相似问题