std :: string追加得太慢?

aks*_*aks 1 c++

我有一种情况,我从结构中读取数据并继续将其附加到字符串,以便它可以通过套接字发送.当数据相当大时,此操作需要花费大量时间.有人可以提出任何替代方案

我有一个结构

struct fileInfo {int file_id; char filename [16]; 双标签; }

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

Pot*_*ter 6

std::ostringstreamfrom <sstream>专为该操作模式而设计.

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

socket.send( my_text.str() ); // get a std::string to handle data
Run Code Online (Sandbox Code Playgroud)