在方法中引用我正在阅读的流
Make a reference in a method to the stream that I'm reading
我在一个名为 total_parsing
的方法中,我创建了一个局部变量
istringstream stream(to_analize)
(其中 to_analize
是我必须分析的字符串)。
当我到达特殊字符 (
或 [
时,我想声明一个方法 reading_between_bracket
,它从左括号到右括号读取字符串。在那之后,我希望 reading_between_bracket
方法让我有可能在总解析中继续阅读。我需要一种方法来读取括号中的字符串,因为每次到达括号时我都想递归地声明它。该字符串采用示例格式。
示例-( B + D -( ( G*F) -H )))
void total_parsing(string to_analize) {
istrinstream stream(to_analize);
do {
// DO OTHER OPERATION WITHOUT BRACKET
if (token == "(" || !token.find('(')) {
stream = reading_between_bracket(stream);
}
} while (stream >> token);
}
istringstream reading_between_bracket(istringstream stream) {
// DO THE OPERATION BETWEEN BRACKET
// RECURSIVE CALL OF THIS METHOD IF I REACH A BRACKET,RETURN THE STREAM
return stream(or a pointer that make possible to continue reading);
}
PS 我必须使用 C++11,我可以利用 STL 和 sstream。
描述中的代码无效
代码无法运行,因为按值传递需要复制,并且 stringstream objects may not be copied. See also。
因此,改为通过引用传递流:
void parse(string input){
istringstream stream(input);
string token;
while (stream >> token) {
if (is_open_bracket(token)) {
parse_inside_brackets(stream);
}
}
}
void parse_inside_brackets(istringstream& stream) {
// notice the '&'
// do some processing here; the stream's internal position is updated
// there is no need to return anything; the change to `stream` will be
// reflected in the caller
}
要完成您描述的递归,您可能需要更像:
void parse(string input) { // helper function to set up recursion
parse_rec(istringstream(input), 0);
}
void parse_rec(istringstream& stream, int level) {
// 'level' param indicates how many nested brackets we've seen
string token;
while (stream >> token) {
if (is_close_bracket(token) { return; }
else if (is_open_bracket(token)) {
parse_rec(stream, level + 1);
}
else {
// handle token at this level
}
}
// If we get here, we ran out of tokens. So if we're not at the base level...
if (level) {
// ... then we had an unmatched bracket, so do error handling
}
}
(但您可能应该阅读更复杂的解析技术:)另一方面,如果您可以用这种方法处理任务,那么也许你甚至不需要递归,只需要跟踪缩进级别。)
我在一个名为 total_parsing
的方法中,我创建了一个局部变量
istringstream stream(to_analize)
(其中 to_analize
是我必须分析的字符串)。
当我到达特殊字符 (
或 [
时,我想声明一个方法 reading_between_bracket
,它从左括号到右括号读取字符串。在那之后,我希望 reading_between_bracket
方法让我有可能在总解析中继续阅读。我需要一种方法来读取括号中的字符串,因为每次到达括号时我都想递归地声明它。该字符串采用示例格式。
示例-( B + D -( ( G*F) -H )))
void total_parsing(string to_analize) {
istrinstream stream(to_analize);
do {
// DO OTHER OPERATION WITHOUT BRACKET
if (token == "(" || !token.find('(')) {
stream = reading_between_bracket(stream);
}
} while (stream >> token);
}
istringstream reading_between_bracket(istringstream stream) {
// DO THE OPERATION BETWEEN BRACKET
// RECURSIVE CALL OF THIS METHOD IF I REACH A BRACKET,RETURN THE STREAM
return stream(or a pointer that make possible to continue reading);
}
PS 我必须使用 C++11,我可以利用 STL 和 sstream。
描述中的代码无效
代码无法运行,因为按值传递需要复制,并且 stringstream objects may not be copied. See also。
因此,改为通过引用传递流:
void parse(string input){
istringstream stream(input);
string token;
while (stream >> token) {
if (is_open_bracket(token)) {
parse_inside_brackets(stream);
}
}
}
void parse_inside_brackets(istringstream& stream) {
// notice the '&'
// do some processing here; the stream's internal position is updated
// there is no need to return anything; the change to `stream` will be
// reflected in the caller
}
要完成您描述的递归,您可能需要更像:
void parse(string input) { // helper function to set up recursion
parse_rec(istringstream(input), 0);
}
void parse_rec(istringstream& stream, int level) {
// 'level' param indicates how many nested brackets we've seen
string token;
while (stream >> token) {
if (is_close_bracket(token) { return; }
else if (is_open_bracket(token)) {
parse_rec(stream, level + 1);
}
else {
// handle token at this level
}
}
// If we get here, we ran out of tokens. So if we're not at the base level...
if (level) {
// ... then we had an unmatched bracket, so do error handling
}
}
(但您可能应该阅读更复杂的解析技术:)另一方面,如果您可以用这种方法处理任务,那么也许你甚至不需要递归,只需要跟踪缩进级别。)