remove char from stringstream and append some data

后端 未结 9 1446
情深已故
情深已故 2020-12-31 01:42

In my code there is a loop that adds sth like that \"number,\" to stringstream. When it ends, I need to extract \',\' add \'}\' and add \'{\' if the loop is to repeated.

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-31 02:00

    You can extract the string (with the str() member), remove the last char with std::string::erase and then reset the new string as buffer to the std::ostringstream.

    However, a better solution would be to not insert the superfluous ',' in the first place, by doing something like that :

    std::ostringstream douCoh;
    const char* separator = "";
    
    douCoh << '{';
    for (size_t i = 0; i < dataSize; ++ i)
    {
      if (v[i].test)
      {
        douCoh << separator << i + 1;
        separator = ",";
      }
    }
    douCoh << '}';
    

提交回复
热议问题