0

To understand the better about what is best among std::string or std::stringstream while is string manipulation involved. The below code can be replaced with std::string and it's append(). Let's consider the below function.

std::string function(const std::vector<std::string>& param1, const std::string& param2)
{
    std::stringstream streamTemp;
    if (!param1.empty())
    {
        for (const auto& item : param1)
        {
            if (streamTemp.tellp() > 0)
            {
                streamTemp<<" || ";
            }
            streamTemp << param2 << "  (" << item << ") ";
        }
        streamTemp<<" (" streamTemp + ") ";
    }
    return streamTemp.str();
}
Build Succeeded
  • 1,153
  • 1
  • 10
  • 24
  • 1
    And your *specific* question is *what*? – Jesper Juhl Jul 06 '20 at 14:36
  • I feel like either is fine. But I'd use `string` just to (potentially) limit the compiled binary size. – JohnFilleau Jul 06 '20 at 14:37
  • 2
    @JesperJuhl: Presumably the one in the question title. – MSalters Jul 06 '20 at 14:37
  • 1
    @MSalters The question title is not clear. You'd of course use a string stream when that's what you need and a string otherwise. It's not at all clear what the question is – Jesper Juhl Jul 06 '20 at 14:39
  • @JesperJuhl based on compiler manipulation of string and stringstream what is best in case of above function operation – Build Succeeded Jul 06 '20 at 14:42
  • @Mannoj: Just to clarify, short of `constexpr` the compiler doesn't do much string manipulation. The Standard Library does most. – MSalters Jul 06 '20 at 15:06
  • 1
    Does this answer your question? [c++ std::ostringstream vs std::string::append](https://stackoverflow.com/questions/19844858/c-stdostringstream-vs-stdstringappend) – underscore_d Jul 07 '20 at 16:45
  • or [std::stringstream vs std::string for concatenating many strings](https://stackoverflow.com/questions/14741144/stdstringstream-vs-stdstring-for-concatenating-many-strings) or [performance of std::string operator + versus stringstream](https://stackoverflow.com/questions/31003445/performance-of-stdstring-operator-versus-stringstream) or etc. I feel like searching would really help you here! – underscore_d Jul 07 '20 at 16:46
  • @underscore_d thanks, but the answers contradict each other and confusing. Not clear to make the decision – Build Succeeded Jul 07 '20 at 17:04
  • @Mannoj Maybe that simply means there is not a 'one size fits all' answer that someone can give you... and you have to analyse the various factors and make your own decision. – underscore_d Jul 08 '20 at 08:40
  • @underscore_d I got your point – Build Succeeded Jul 08 '20 at 09:36

1 Answers1

0

With strings, you can append char, char[] or another std::string. But std::stringstream you can append any type that has a suitable operator<<, such as int and float.

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • I want to understand performance-wise as mentioned in the provided function. I dont have other primitive involved. – Build Succeeded Jul 06 '20 at 14:40
  • 2
    @Mannoj performance can be measured but almost never predicted accurately. If you want to know what performs better then you can compare two implementations – 463035818_is_not_an_ai Jul 06 '20 at 14:41
  • @idclev463035818 want to learn about the stream vs string handling by compiler. may be not exact answer but can lead to better link. – Build Succeeded Jul 06 '20 at 14:44
  • @Mannoj: C++ has very little magic involved. The compiler isn't involved, but the Standard Library is. In both cases it needs to concatenate characters, and that's the time-consuming step. `std::stringstream` provides a thin wrapper which helps to convert a `float` to digits, but you're not doing that so its performance doesn't matter. – MSalters Jul 06 '20 at 14:47
  • 1
    @Mannoj there are different aspects to this: A) readability and expressiveness of your code B) what the compiler does to your code C) performance of the resulting executable. They are related but not the same. While writing code the one you should focus on is A – 463035818_is_not_an_ai Jul 06 '20 at 14:54
  • @idclev463035818 I do agree with your view. And yes exactly while doing that I am not able to identify what is best for me in function. I got comment "can we use "string". So I do not have any answer. – Build Succeeded Jul 06 '20 at 15:03