我有一个字符串向量,其中包含一些大于500的字符串.我使用openssl函数,需要缓冲区进行加密/解密.鉴于我以这种方式使用字符串向量和缓冲区,在进行此转换的空间和时间方面,最佳算法是什么.可以假设每个字符串少于200个字符.
目前,我正在提取路径中的每个条目,连接字符串,调用.c_str()方法和使用strcpy从函数中提取.
void copy(vector<string>& paths, unsigned char** plainTextBuffer, size_t& p_len){
int size = paths.size();
int i = 0;
string temp = "";
for(i=0; i<size; i++){
temp+= paths[i];
temp+= "\n";
}
p_len = temp.length();
(*plainTextBuffer) = malloc(p_len + 1);
strcpy((*plainTextBuffer), temp.c_str());
return;
}
Run Code Online (Sandbox Code Playgroud)
是否有任何内置工具可以更好,更快地完成这项工作?(我已从此代码段中排除了错误检查和转换)
编辑:
我在动画片中添加了+1.我要求从初始条件到预期输出的最小复杂性方式.我正在使用malloc,因为我使用的是一个简单的缓冲区,它比新的更快.
编辑2:
感谢我的一些评论,我正在删除中间人的一些复制,并有以下内容
void copy(vector<string>& paths, unsigned char** plainTextBuffer, size_t& p_len){
int size = paths.size(), total = 0, i = 0;
for(i=0; i<size; i++){
total+= paths[i].size() + 1;
}
p_len = total; …Run Code Online (Sandbox Code Playgroud)