防止循环内外的代码重复
我在重写循环时遇到问题:
else if( "d" == option || "debug" == option )
{
debug(debug::always) << "commandline::set_internal_option::setting debug options: "
<< value << ".\n";
string::size_type index = 0;
do
{
const string::size_type previous_index = index+1;
index=value.find( ',', index );
const string item = value.substr(previous_index, index);
debug::type item_enum;
if( !map_value(lib::debug_map, item, item_enum) )
throw lib::commandline_error( "Unknown debug type: " + item, argument_number );
debug(debug::always) << "commandline::set_internal_option::enabling " << item
<< " debug output.\n";
debug(debug::always) << "\n-->s_level=" << debug::s_level << "\n";
debug::s_level = static_cast<debug::type>(debug::s_level ^ item_enum);
debug(debug::always) << "\n-->s_level=" << debug::s_level << "\n";
} while( index != string::npos );
}
value
类似于 string("commandline,parser")
,问题是在第一次运行时,我需要 substr (previous_index, index)
,但在后续的每次迭代中,我需要 substr(previous_index+1, index)
来跳过逗号。是否有一些我忽略的简单方法,或者我是否必须在初始迭代的循环外部重复调用 find
?
I have a problem rewriting a loop:
else if( "d" == option || "debug" == option )
{
debug(debug::always) << "commandline::set_internal_option::setting debug options: "
<< value << ".\n";
string::size_type index = 0;
do
{
const string::size_type previous_index = index+1;
index=value.find( ',', index );
const string item = value.substr(previous_index, index);
debug::type item_enum;
if( !map_value(lib::debug_map, item, item_enum) )
throw lib::commandline_error( "Unknown debug type: " + item, argument_number );
debug(debug::always) << "commandline::set_internal_option::enabling " << item
<< " debug output.\n";
debug(debug::always) << "\n-->s_level=" << debug::s_level << "\n";
debug::s_level = static_cast<debug::type>(debug::s_level ^ item_enum);
debug(debug::always) << "\n-->s_level=" << debug::s_level << "\n";
} while( index != string::npos );
}
value
is something like string("commandline,parser")
and the problem is that in the first run, I need substr(previous_index, index)
, but in every subsequent iteration I need substr(previous_index+1, index)
to skip over the comma. Is there some easy way I'm overlooking or will I have to repeat the call to find
outside the loop for the initial iteration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于您的目标是防止代码重复:
如果您想创建自己的 split 函数,您可以执行以下操作:
Since your goal is to prevent code duplication:
If you want to create your own split function, you can do something like this:
为什么不在获取
substr
后更新previous_index
?未经检查,但这应该可以解决问题(只需多记住一个单词)。
Why not update
previous_index
after taking thesubstr
?Unchecked, but this should do the trick (with only one more word of memory).
从
-1
开始?Start at
-1
?一个愚蠢的(而且有点不可读)的解决方案是这样的:
A stupid (and somewhat unreadable) solution would be something like this:
首先,我认为有一个小错误:
在您的代码中,表达式
index=value.find( ',', index );
不会更改index
的值如果它已经是字符串中逗号字符的索引(除了第一次循环迭代之外总是这种情况)。因此,您可能需要将
while(index != string::npos );
替换为while(index++ != string::npos );
和previous_index = index +1
与previous_index = index
。这也应该可以解决您原来的问题。
澄清一下:
First, I think there's a small error:
In your code, the expression
index=value.find( ',', index );
doesn't change the value ofindex
if it already is the index of a comma character within the string (which is always the case except for the first loop iteration).So you might want to replace
while( index != string::npos );
withwhile( index++ != string::npos );
andprevious_index = index+1
withprevious_index = index
.This should also solve your original problem.
To clarify: