gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
c++ vector转换为带分隔的字符串
- User
c++ vector转换为带分隔的字符串
- Assistant
在C++中可以使用ostringstream和auto来实现。
示例代码:
#include <iostream> #include <sstream> #include <vector> using namespace std; int main() { vector<int> v = { 1, 2, 3, 4, 5 }; ostringstream oss; for (auto& i : v) { oss << i << ","; } string str = oss.str(); str.pop_back(); // 去掉最后一个逗号 cout << str << endl; return 0; }这段代码会输出:
1,2,3,4,5。