简体   繁体   English

C++ 转换向量<std::string>字符*</std::string>

[英]C++ convert vector<std::string> to char*

Hello as the title suggests I want to convert a vector,你好,正如标题所示,我想转换一个向量,

 std::vector<std::string>

to a c-style string, like到 c 风格的字符串,比如

char* buffer.

The reason I want the vector to become a c-style string is because I am currently working with the WinApi, and I am specifically trying to use我希望向量成为 c 风格字符串的原因是因为我目前正在使用 WinApi,我特意尝试使用

SetWindowTextA()

which does not take a vector.它不带向量。 And yes, I have to read the data in to a string vector first so there's not really anything I can change there.是的,我必须先将数据读入字符串向量,所以我无法在其中更改任何内容。 So if you could help me or point me in the right direction I'd be more than happy因此,如果您能帮助我或为我指明正确的方向,我会非常高兴

EDIT: To further explain: Yes I will get several string loaded in to the vector.编辑:进一步解释:是的,我会将几个字符串加载到向量中。 I simply want all those strings to combine in to one string.我只是希望所有这些字符串组合成一个字符串。

Greetings, ye546问候,ye546

So just combine the vector strings into a single string, then call c_str() to convert to a char*.所以只需将向量字符串组合成一个字符串,然后调用 c_str() 转换为 char*。

std::vector<std::string> vec;
...
std::string combined;
for (const auto& s : vec)
    combined += s;
WinAPI(combined.c_str());

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

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