ostream & print(ostream &os, const string& s, char c)
{
os << s << c;
return os;
}
int main()
{
vector<string> words{"helo", "world", "this", "is", "C++11"};
ostringstream os;
char c = ' ';
for_each(words.begin(), words.end(),
[&os, c](const string & s){os << s << c;} );
cout << os.str() << endl;
ostringstream os1;
// ostream不能拷贝,若希望传递给bind一个对象,
// 而不拷贝它,就必须使用标准库提供的ref函数
for_each(words.begin(), words.end(),
bind(print, ref(os1), _1, c));
cout << os1.str() << endl;
}
c++ function bind
最新推荐文章于 2025-03-09 12:16:09 发布
本文通过示例展示了如何在C++11中利用ostream进行字符串拼接及输出,包括使用lambda表达式和std::bind进行迭代输出的方法。
2162

被折叠的 条评论
为什么被折叠?



