jeudi 23 mai 2019

How to return the value of foreach loop in c++?

I need to take multiple lines of string and then prepends with spaces.

I am using boost::split to split the string and assign the value to a vector. And then inside for-each loop, I add spaces to the vector value.

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;
  }

I should get the return value as below. cout result is correct but the return value txt is unable to get.

      This is a sentence.
      And another sentence

Aucun commentaire:

Enregistrer un commentaire