std::stringstream as parameter to a function

拥有回忆 提交于 2019-12-05 19:24:08

If, by writing return ss.str; you intend to call the str member function from std::stringstream, then you are missing a pair of parenthesis :

return ss.str();

Also, your code probably won't do what you expect. If you want every call to addup to work on the same std::stringstream instance, you have to take it by reference : modify the addup signature and add a boost::ref() around the ss parameter in the boost::bind.

Here is a working version which I presume does what you expect :

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

An alternative using boost::lambda :

std::for_each(temp_results.begin(), temp_results.end(), ss << boost::lambda::_1 << ';');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!