简体   繁体   中英

How to convert from LPWSTR** to std::vector<std::wstring>

I have a function that works with std::vector, declared like this:

void MyFunc(std::vector<std::wstring> &vFillArray){
//....
//fill the vector with wstrings
//....
}

Now I would like to export my function in a DLL library and make it available to a vb 6 application. Using the library as it is, would crash the vb 6 application. I have to change the declaration to LPWSTR** (or wchar_t** I think) but now, how to reconvert this type internally in my c++ function? Internally I use std::vector to fill the vector with string. Any advice?

Assuming LPWSTR is the same as wchar_t , you can use the usual iterator-assignment constructors and functions of std::vector :

wchar_t const *raw_data[] = {L"Hello", L"World", L"Test"};
std::vector<std::wstring> vec(raw_data, raw_data+2); // construct with first 2 elements
std::wcout << vec[0] << ' ' << vec[1] << '\n';
wchar_t const ** raw_ptr = raw_data;
vec.assign(raw_ptr, raw_ptr+3); // assing 3 elements
std::wcout << vec[0] << ' ' << vec[1] << ' ' << vec[2] << '\n';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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