【问题标题】:How to return the value of foreach loop in c++?如何在 C++ 中返回 foreach 循环的值?
【发布时间】:2019-10-09 22:10:28
【问题描述】:

我需要多行字符串,然后在前面加上空格。

我正在使用 boost::split 拆分字符串并将值分配给向量。然后在 for-each 循环中,我在向量值中添加空格。

static string test(){

    std::string text = "This is a sentence.\nAnd another sentence";
    string nl = "\n";
    string pad="     ";
    string txt;
    std::vector<std::string> results;

    boost::split(results, text, boost::is_any_of(nl));

    for(string line : results){
      txt = pad + line + nl;
      cout << txt;
    }
    return txt;
  }

我应该得到如下返回值。 cout结果正确但无法获取返回值txt。

      This is a sentence.
      And another sentence

【问题讨论】:

  • 你得到了什么?
  • 我建议你对你的代码做一些rubber duck debugging,尤其是你有的那个循环。
  • 您的代码在每次循环迭代时重新初始化txt,将其分配为一个整体。试试txt.append(pad + line + nl)
  • 我认为要让人们真正理解他们的错误,我们应该停止在零时间内为这类问题提供解决方案。我们应该让 OP 去阅读和思考像 Steve 和 Someprogrammer dude 那样的 cmets。

标签: c++ string c++11 boost


【解决方案1】:

拥有

 for(string line : results){
   txt = pad + line + nl;
   cout << txt;
 }
 return txt;

返回 txt 是循环中 pad + line + nllast 值,如果 results 为空,则返回一个空字符串

cout结果正确但无法获取返回值txt。

可能意味着您只需要折叠每个字符串并返回结果:

string r;

for(string line : results){
  txt = pad + line + nl;
  cout << txt;
  r += txt;
}
return r;

或类似的东西可能没有 pad

【讨论】:

  • 谢谢。这将返回我想要的结果。
  • 或者只是在末尾附加txtcout。不需要额外的副本。
  • @LightnessRacesinOrbit 是的,我知道,我想接近原始代码,以帮助 OP 了解他的解决方案中的问题;-)
  • @LightnessRacesinOrbit 如果您真的想要更简单的代码,只需编写并返回一个文字字符串,不需要所有其余的 ^^
【解决方案2】:

另一种方式:

static string test(){
    std::string text = "This is a sentence.\nAnd another sentence\n";
    std::string result = "   " + boost::algorithm::replace_all_copy(s, "\n", "\n    ");
    std::cout << result;
    return result;
}

【讨论】:

  • 我可以认为这不是 OP 想要做的吗?否则没有理由使用 boost 但直接写入/返回预期的文字字符串 ^^
【解决方案3】:

你的循环:

for(string line : results){
  txt = pad + line + nl;
  cout << txt;
}
return txt;

在每次循环迭代时重置txt(并且您一次打印该迭代的文本)。然后,您只有在循环之后剩余的最终迭代值。

相反,追加txt 以保留所有值。因此,您还需要将cout 提升到循环之外:

for(string line : results){
  txt += pad + line + nl;
}
cout << txt;
return txt;

【讨论】:

    【解决方案4】:

    我将展示已经构建矢量的函数部分。因此,我将展示的函数被简化,但演示了如何构建和返回结果字符串。

    #include <iostream>
    #include <string>
    #include <vector>
    
    static std::string test( const std::vector<std::string> &results )
    {    
        const std::string pad( "     " );
    
        std::string::size_type n = 0;
        for ( const std::string &s : results ) n += s.length();
    
        n += results.size() * ( pad.size() + sizeof( '\n' ) );
    
        std::string txt;
        txt.reserve( n );
    
        for ( const std::string &s : results ) txt += pad + s + '\n';
    
        return txt;
    }
    
    int main()
    {
        std::vector<std::string> results = { "This is a sentence.", "This is a sentence." };
    
        std::cout << test( results );
    }
    

    程序输出是

     This is a sentence.
     This is a sentence.
    

    为了使代码更有效,您应该为字符串txt 保留足够的空间。循环

        for ( const std::string &s : results ) n += s.length();
    

    计算存储在向量中的所有字符串的总大小。

    注意:您可以使用在标头&lt;numeric&gt; 中声明的标准算法std::accumulate,而不是例如循环

    std::string::size_type n = std::accumulate( std::begin( results ), std::end( results ), 
                                                std::string::size_type( 0 ),
                                                []( const std::string::size_type acc, const std::string &s )
                                                {
                                                    return acc + s.size();
                                                } );
    

    然后添加每个字符串的填充空格长度和字符'\n'的长度。

    因此,一切都准备好构建从函数返回的结果字符串。

    std::string txt;
    txt.reserve( n );
    
    for ( const std::string &s : results ) txt += pad + s + '\n';
    
    return txt;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多