简体   繁体   中英

Converting from vector<string> to wchar_t**

std::vector<std::string> elems = split(command, ' ');
const int argc = elems.size();
wchar_t** argv = new wchar_t*[argc]();
//wchar_t* argv[10];
for (int i = 0; i < argc; i++) {
    const char* arg = elems[i].c_str();
    int size = strlen(arg);
    size_t length = 0;
    wchar_t* e = new wchar_t[size];
    mbstowcs_s(&length, e, size + 1, arg, size);
    argv[i] = e;
}

This is my code attempting to convert a vector of strings to a wchar_t**. When I comment out the third line and uncomment the fourth line, it works. But I want my wchar_t** to persist, so I want to use the third line and not the fourth. Please explain to me why the third line does not work as expected.

You're allocating new wchar_t[size] but copying size + 1 characters into it. That's undefined behavior.

You can convert from string to wstring like this:

std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cv;
auto warg = cv.from_bytes(arg);
auto wargv = warg.c_str();  // wchar_t*

But you may also consider passing vector instead of int and wchar_t**:

std::vector<std::wstring> args;
for(auto& elm : elms)
{
    std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> cv;
    args.push_back(cv.from_bytes(elm));
}

This is basically a retrofit of the answer found here .

Basically what you're looking for is a std::vector<wchar_t*> (one of the few times ever to use a vector of character pointers), and sent that to the function that requires a wchar_t** .

#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>

class CommandLine
{
    typedef std::vector<wchar_t> CharArray;
    typedef std::vector<CharArray> ArgumentVector;
    ArgumentVector argvVec;
    std::vector<wchar_t *> argv;
public:
    CommandLine(const std::string& cmd);
};

void my_command_line(int numArgs, wchar_t** args);

CommandLine::CommandLine(const std::string& cmd)
{
    std::string arg;
    std::istringstream iss(cmd);
    while (iss >> arg)
    {
        size_t length = 0;
        CharArray cArray(arg.size() + 1);

        mbstowcs_s(&length, cArray.data(), arg.size() + 1, arg.c_str(), arg.size());

        argvVec.push_back(CharArray(arg.begin(), arg.end()));

        // make sure we null-terminate the last string we added.
        argvVec.back().push_back(0);

        // add the pointer to this string to the argv vector
        argv.push_back(argvVec.back().data());
    }

    // call the alternate command-line function
    my_command_line(argv.size(), argv.data());
}

void my_command_line(int numArgs, wchar_t** args)
{
    for (int i = 0; i < numArgs; ++i)
        std::wcout << "argument " << i << ": " << args[i] << std::endl;
}

int main()
{
    CommandLine test("command1 command2");
}

Live Example

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