c++ - std::string 附加太慢?

标签 c++

我有这样一种情况,我从一个结构中读取数据并将其附加到一个字符串中,以便它可以通过套接字发送。当数据相当大时,这个操作会花费很多时间。有人可以建议任何替代方案吗?

我有一个结构

struct fileInfo { int file_id; char filename[16]; double tag; }

我做了一个 std::stringstream >>file_id 和 stringstream.str() 这对每个值重复,附加到字符串并发送

最佳答案

std::ostringstream来自 <sstream>专为这种操作模式而设计。

ostringstream my_text;
my_text << "hello " << 2 << foo << endl; // efficiently catenate

socket.send( my_text.str() ); // get a std::string to handle data

关于c++ - std::string 附加太慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3650074/

相关文章:

c++ - main() 中下面 'i' 的类型。为什么是int&?

c++ - 如何在英特尔实感(Visual C++)中保存图像

c++ - 无法在 C++ 中遍历二维数组

c++ - 使用 C++ 递归扫描目录上的 ACL

c++ - 在 C++ fstream 中更新文件末尾

c++ - Mac 地址 - C++ - 平台无关

c++ - GHashTable 的大小/调整大小

c++ - Borland C++ : Ambiguity with std (error E2015)

c++ - 如何为点云赋予纹理?

c++ - 如何找到 2 组的交集?