我想制作受变量变化(c ++)影响的字符串

I want to make string which is influenced by variables changing (c++)

本文关键字:影响 字符串 变化 变量      更新时间:2023-10-16

我在字符串中的变量中使用字符串流,例如

int c;
stringstream ss;
string st;

ss << "some texts" << c;
st=ss.str();
cout << st;

但是当我更改 c 并再次回调 ss.str(( 时,ss.str(( 正在保存前国际 C,不是新的。是否有任何函数或字符串方式受到此处更改变量的影响?

字符串流不绑定到变量,你有 2 个选项

1(创建自己的绑定到int的类(保存引用或其他东西(

2(像清除字符串流一样

ss.str(""); // clear stream

然后再次写信给它

ss << "some texts" << c; // set the stream again with modified c