简体   繁体   English

如何转换矢量 <string> 到一个向量 <char*>

[英]How to convert a vector<string> to a vector<char*>

This is the opposite of How to convert a vector<char*> to a vector<string>/string question. 这与如何将vector <char *>转换为vector <string> / string问题相反。

I have some legacy routine which works with vector<char*> so I need to transform my vector<string> . 我有一些遗留的例程,它与vector<char*>所以我需要转换vector<string>

Here is what I come out with: 这是我提出来的:

std::vector<char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
    charVec[i]= new char(strVec[i].size()+1); 
    charVec[i][strVec[i].copy(charVec[i], strVec[i].size())] = '\0';
}

Is this correct? 这个对吗?

Is there a better way to implement it? 有没有更好的方法来实现它?


ps of course at the end I have: ps当然最后我有:

for (int i=0; i<strVec.size();i++) {
    delete charVec[i];
}

A faster way of doing this is 更快的方法是这样做

std::vector<const char*> charVec(strVec.size(),nullptr);
for (int i=0; i<strVec.size();i++) {
    charVec[i]= strVec[i].c_str();
}

and then using the resulting vector. 然后使用得到的矢量。 This will save a lot of time on memory allocation for large sets of data. 这将为大型数据集的内存分配节省大量时间。

Why are you allocating new buffer for your strings? 为什么要为字符串分配新的缓冲区? You are passing a vector of character pointers to a function and finally deleting allocated buffers. 您正在将一个字符指针vector传递给函数,最后删除已分配的缓冲区。 It means these buffers are temporary and there is no need to allocate memory for them as far as source vector ( vector<string> ) exist. 这意味着这些缓冲区是临时的,只要存在源vectorvector<string> )就不需要为它们分配内存。

You can easily do this: 你可以轻松地做到这一点:

std::vector<std::string> vec1 = { "1", "2", "3" };
std::vector<const char*> vec2;

vec2.resize(vec1.size(), nullptr);

std::transform(std::begin(vec1), std::end(vec1), std::begin(vec2), [&](const std::string& str)
{
    return str.c_str();
});

Your new statement looks wrong. 你的new陈述看起来不对劲。 You want to allocate an array of char long enough to hold the string. 你想要分配一个足够长的char数组来保存字符串。 You're allocating a single char whose value is the length of the string. 您正在分配一个char其值是字符串的长度。 I'm surprised you didn't get compiler warnings about that. 我很惊讶你没有得到编译器警告。

Try this: 尝试这个:

std::vector<char*> charVec;
for (const auto &str : strVec) {
  char *charStr = new char[str.size() + 1];
  std::strcpy(charStr, str.c_str());
  charVec.push_back(charStr); 
}

The only drawback here is that, if one of the strings has embedded null characters, nothing beyond those will be copied. 这里唯一的缺点是,如果其中一个字符串嵌入了空字符,那么除此之外的任何内容都不会被复制。 But I suspect that a function which takes a vector of char * almost certainly doesn't care about that anyway. 但是我怀疑一个带有char *向量的函数几乎肯定不关心那个。

You could simply use string::c_str() to get the string out of a std::string. 你可以简单地使用string :: c_str()从std :: string中获取字符串。 Read more in How to convert a std::string to const char* or char*? 阅读更多如何将std :: string转换为const char *或char *?

Or you could just modify a bit the body of the function, but only you know if this is worth it. 或者你可以修改一下函数的主体,但只有你知道这是否值得。


Of course as ssell mentions in the comments, keep in mind that with this approach you need not to destroy your strings when you handle them! 当然,正如ssell在评论中提到的那样,请记住,使用这种方法时,您不需要在处理它们时销毁它们! It's a trade-off, you avoid the copy (so cool), but you need to be careful not reading junk (in case you delete your strings)! 这是一个权衡,你避免复制(这么酷),但你需要小心不要阅读垃圾(如果你删除你的字符串)!

vector < string > to vector < const char* > : use c_str() vector <string> to vector <const char *>:use c_str()

std::vector<const char*> charVec(strVec.size(), nullptr);
for (int i=0; i<strVec.size(); i++) {
    charVec[i] = strVec[i].c_str();
}

vector < string > to vector < char* > : use &x[0] vector <string> to vector <char *>:use&x [0]

std::vector<char*> charVec(strVec.size(), nullptr);
for (int i=0; i<strVec.size(); i++) {
    charVec[i] = &strVec[i][0];
}

Refs: How to convert a std::string to const char* or char*? 参考: 如何将std :: string转换为const char *或char *?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM