Std :: stringstream as parameter for function

I have std::vector<std::string> temp_resultsone and I want to use std :: for_each to traverse this vector and concatenate the string, so I came up with the following construct:

std::stringstream ss;
std::string res = std::for_each(temp_results.begin(), temp_results.end(), boost::bind(addup, _1, ss));

std::string addup(std::string str, std::stringstream ss)
{
    ss << str;
    ss << ";";

    return ss.str;
}

I get the following error message:

error C2475: 'std::basic_stringstream<_Elem,_Traits,_Alloc>::str' : forming a pointer-to-member requires explicit use of the address-of operator ('&') and a qualified name
        with
        [
            _Elem=char,
            _Traits=std::char_traits<char>,
            _Alloc=std::allocator<char>
        ]

Can someone explain what is wrong?

+3
source share
1 answer

If, after writing return ss.str;, you intend to call a member function strfrom std::stringstream, then you are missing a pair of brackets:

return ss.str();

, , , , . , addup std::stringstream, : addup boost::ref() ss boost::bind.

, , , :

void addup(std::string str, std::stringstream &ss)
{
    ss << str;
    ss << ";";
}

int main() 
{
    std::vector<std::string> temp_results;
    /* ... */

    std::stringstream ss;
    std::for_each(temp_results.begin(), temp_results.end(), boost::bind(addup, _1, boost::ref(ss)));
    std::cout << ss.str() << std::endl;
}

boost::lambda:

std::for_each(temp_results.begin(), temp_results.end(), ss << boost::lambda::_1 << ';');
+4

Source: https://habr.com/ru/post/1780376/


All Articles