防止循环内外的代码重复

发布于 2024-12-22 02:56:05 字数 1363 浏览 3 评论 0原文

我在重写循环时遇到问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

万劫不复 2024-12-29 02:56:05

由于您的目标是防止代码重复:

std::vector<std::string> v;
boost::split(v, value, [](char c) { c == ','; });

如果您想创建自己的 split 函数,您可以执行以下操作:

template<typename PredicateT>
std::vector<std::string> Split(const std::string & in, PredicateT p)
{
    std::vector<std::string> v;
    auto b = in.begin();
    auto e = b;
    do {
        e = std::find_if(b, in.end(), p);
        v.emplace_back(b,e);
        b = e + 1;        
    } while (e != in.end());

    return v;
}

Since your goal is to prevent code duplication:

std::vector<std::string> v;
boost::split(v, value, [](char c) { c == ','; });

If you want to create your own split function, you can do something like this:

template<typename PredicateT>
std::vector<std::string> Split(const std::string & in, PredicateT p)
{
    std::vector<std::string> v;
    auto b = in.begin();
    auto e = b;
    do {
        e = std::find_if(b, in.end(), p);
        v.emplace_back(b,e);
        b = e + 1;        
    } while (e != in.end());

    return v;
}
九公里浅绿 2024-12-29 02:56:05

为什么不在获取 substr 后更新 previous_index

string::size_type index = 0;
string::size_type previous_index = 0;
do {
  index=value.find( ',', previous_index );
  const string item = value.substr(previous_index, index);
  previous_index = index+1;
} while( index != string::npos );

未经检查,但这应该可以解决问题(只需多记住一个单词)。

Why not update previous_index after taking the substr?

string::size_type index = 0;
string::size_type previous_index = 0;
do {
  index=value.find( ',', previous_index );
  const string item = value.substr(previous_index, index);
  previous_index = index+1;
} while( index != string::npos );

Unchecked, but this should do the trick (with only one more word of memory).

浅唱々樱花落 2024-12-29 02:56:05

-1 开始?

string::size_type index = -1;
do
{
    const string::size_type previous_index = index + 1;
    index=value.find(',', previous_index);
    const string item = value.substr(previous_index, index - previous_index);
} while( index != string::npos );

Start at -1?

string::size_type index = -1;
do
{
    const string::size_type previous_index = index + 1;
    index=value.find(',', previous_index);
    const string item = value.substr(previous_index, index - previous_index);
} while( index != string::npos );
一人独醉 2024-12-29 02:56:05

一个愚蠢的(而且有点不可读)的解决方案是这样的:

string::size_type once = 0;
/* ... */
const string::size_type previous_index = index+1 + (once++ != 0); // or !!once

A stupid (and somewhat unreadable) solution would be something like this:

string::size_type once = 0;
/* ... */
const string::size_type previous_index = index+1 + (once++ != 0); // or !!once
清秋悲枫 2024-12-29 02:56:05

首先,我认为有一个小错误:

在您的代码中,表达式 index=value.find( ',', index ); 不会更改 index 的值如果它已经是字符串中逗号字符的索引(除了第一次循环迭代之外总是这种情况)。

因此,您可能需要将 while(index != string::npos ); 替换为 while(index++ != string::npos );previous_index = index +1previous_index = index

这也应该可以解决您原来的问题。

澄清一下:

string::size_type index = 0;
do
{
    const string::size_type previous_index = index;
    index = value.find( ',', index );
    const string item = value.substr(previous_index, index - previous_index);
} while( index++ != string::npos );

First, I think there's a small error:

In your code, the expression index=value.find( ',', index ); doesn't change the value of index 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 ); with while( index++ != string::npos ); and previous_index = index+1 with previous_index = index.

This should also solve your original problem.

To clarify:

string::size_type index = 0;
do
{
    const string::size_type previous_index = index;
    index = value.find( ',', index );
    const string item = value.substr(previous_index, index - previous_index);
} while( index++ != string::npos );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文