传递std :: vector< std :: string>作为C风格字符串数组(char **) [英] Passing std::vector<std::string> as array of C-style strings (char**)
本文介绍了传递std :: vector< std :: string>作为C风格字符串数组(char **)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想传递一个std :: strings的向量到OpenGL作为C风格的null终止字符数组的数组(const char **)。
I'd like to pass a vector of std::strings to OpenGL as array of C-style null terminated character arrays (const char**).
void glTransformFeedbackVaryings(GLuint program,
GLsizei count,
const char **varyings,
GLenum bufferMode);
是否有可能?
推荐答案
首先使用 std :: string
的 .c_str()
到 const char *
的。
std::vector<const char*> c_strs;
std::transform(std::begin(strings), std::end(strings),
std::back_inserter(c_strs), std::mem_fn(&std::string::c_str));
然后通过 .data()
向量的成员:
glTransformFeedbackVaryings( ... , c_strs.size(), c_strs.data(), ...);
用您的其他参数替换...。注意:我没有编译这个代码,但它应该很好(理论上)。
replacing the ...'s with your other parameters. Note: I haven't compiled this code, but it should be fine (in theory.)
如果空间对你很重要,你可以在向量中保留空间提前与
If space is really important to you, you can reserve space in the vector ahead of time with
c_strs.reserve(strings.size());
或转换后使用
c_strs.shrink_to_fit();
这篇关于传递std :: vector< std :: string>作为C风格字符串数组(char **)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文