C++ 如何将ostringstream对象复制到另一个对象中(使用gcc-4.7.0)
所有其他帖子都告诉我要更改编译器,但我不能,因为我应该使用这个编译器。请帮忙C++ 如何将ostringstream对象复制到另一个对象中(使用gcc-4.7.0),c++,gcc,ostringstream,C++,Gcc,Ostringstream,所有其他帖子都告诉我要更改编译器,但我不能,因为我应该使用这个编译器。请帮忙 void foo(ostringstream &os) { ostringstream temp; temp << 0; //do something os.swap(temp); } void foo(ostringstream&os){ ostringstream温度; temp您可以使用中的成员函数从临时流中获取缓冲区,并将缓冲区设置为传递给foo #incl
void foo(ostringstream &os) {
ostringstream temp;
temp << 0;
//do something
os.swap(temp);
}
void foo(ostringstream&os){
ostringstream温度;
temp您可以使用中的成员函数从临时流中获取缓冲区,并将缓冲区设置为传递给foo
#include <iostream>
#include <string>
#include <sstream>
void foo(std::ostringstream &os)
{
std::ostringstream temp;
temp << "goodbye";
//do something
os.str(temp.str()); // Set the new buffer contents
}
int main()
{
std::ostringstream out;
out << "hello";
std::cout << out.str() << std::endl;
foo(out);
std::cout << out.str() << std::endl;
}
很难提供一个解决方案,而不知道您试图在代码中完成什么。请编辑您的帖子,并在其中包含相关的代码部分。您发布的示例中的简单解决方案是直接使用os
,而不是第二个temp
流,该流随后必须复制到os
。
void foo(std::ostringstream &os)
{
os.str(""); // Set the new buffer contents
os << "goodbye";
// do something
}