Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

winapi - 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

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

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

question from:https://stackoverflow.com/questions/65645667/c-convert-vectorstdstring-to-char

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So just combine the vector strings into a single string, then call c_str() to convert to a char*.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...