[英]Convert std::vector<std::string> to const char* const*
I'm working with Vulkan, and I'm trying to pass my validation layer as a vector<string>
. 我正在使用Vulkan,并且正在尝试将验证层作为
vector<string>
传递。 But the field ppEnabledLayerNames
from VkInstanceCreateInfo only takes const char* const*
. 但是
ppEnabledLayerNames
的ppEnabledLayerNames字段仅采用const char* const*
。 I don't understand the type and how to convert my vector's data into it. 我不了解类型以及如何将向量的数据转换为它。
std::vector<std::string> v = {
"VK_LAYER_LUNARG_standard_validation"
};
const char* const* data = std::accumulate(std::next(v.begin()), v.end(),
v[0],
[](std::string a, std::string b) {
return a + b;
}).c_str();
But when I compile, I got this : 但是当我编译时,我得到了:
error: cannot convert ‘const char*’ to ‘const char* const*’ in assignment
}).c_str();
Here's a way to do it on a std::string
: 这是在
std::string
上执行此操作的方法:
const std::string str = "test";
const char* str_p = str.c_str();
const char* const* ppEnabledExtensionNames = &str_p;
Be careful if c_str()
is part of a temporary string that's getting trashed. 如果
c_str()
是要删除的临时字符串的一部分,请小心。 You may need to retain a reference to the base string object until you're done with this. 在完成此操作之前,您可能需要保留对基本字符串对象的引用。
With c_str
you will get const char*
. 使用
c_str
您将获得const char*
。 A const char* const*
is an array of const char*
. const char* const*
是const char*
的数组。
vector<string> vectorOfStrings = {
"Aa",
"Bb",
"Cc"
};
vector<const char*> vectorOfCStyleStrings(3);
// convert from string to C style strings
for (string item : vectorOfStrings)
vectorOfCStyleStrings.push_back(item.c_str());
// get vector like a const C style array
const char* const* arrayOfCStyleStrings = vectorOfCStyleStrings.data();
This is what you're doing in your snippet: 这是您在代码段中所做的:
v
(or crash if v
is empty); v
所有字符串(如果v
为空,则崩溃); const char
to const char* const*
. const char
的悬空指针分配给const char* const*
。 The last part did not compile, but that's a good thing - dealing with this in a runtime would be a sad expirience. 最后一部分没有编译,但这是一件好事-在运行时处理这个问题会令人遗憾。
By the looks of the interface (namely the ppEnabledLayerNames
parameter) one could guess that you don't want to concatenate the strings in the first place, but instead you want to pass an array of c-strings. 通过界面的外观(即
ppEnabledLayerNames
参数),您可能会猜到您不想首先串联字符串,而是想传递一个c字符串数组。
Here is how to get it from vector<string>
: 这是从
vector<string>
获取它的方法:
vector<const char*> layer_names(v.size());
std::transform(v.begin(), v.end(), layer_names.begin(), [](const string& s)
{ return s.c_str(); });
const char* const* data = layer_names.data();
Notice that it will only be valid until either layer_names
or v
is destroyed. 注意,它只有在
layer_names
或v
被销毁之前才有效。
The better way would be to find a C++-friendly interface for Vulkan, I can't help with that though. 更好的方法是找到Vulkan的C ++友好接口,但是我对此无能为力。
I finally found the solution this morning, using std::accumulate
我终于今天早上使用
std::accumulate
找到了解决方案
auto d = std::accumulate(
std::next(v.begin()), v.end(),
v[0],
[](std::string a, std::string b) {
return a + b;
}).c_str();
const char* const* data = &d;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.