std::stringstream 作为函数的参数

发布于 2024-10-07 09:02:21 字数 814 浏览 0 评论 0原文

我有一个 std::vector; temp_results 并且我希望使用 std::for_each 来遍历这个向量并连接一个字符串,所以我炮制了以下构造:

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

我收到以下错误,这超出了我的理解:

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>
        ]

有人可以解释一下出了什么问题吗?

I have a std::vector<std::string> temp_results and I wish to use std::for_each to go through this vector and concatenate a string, so I concocted the following construction:

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, which is beyond my understanding:

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>
        ]

Could someone please explain what is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灵芸 2024-10-14 09:02:21

如果通过编写 return ss.str; 您打算从 std::stringstream 调用 str 成员函数,那么您就缺少一对括号:

return ss.str();

此外,您的代码可能不会达到您的预期。如果您希望对 addup 的每次调用都在同一个 std::stringstream 实例上工作,则必须通过引用来获取它:修改 addup签名并在 boost::bind 中的 ss 参数周围添加 boost::ref()

这是一个工作版本,我认为它符合您的期望:

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 << ';');

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 << ';');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文