简体   繁体   English

从向量转换 <string> 到wchar_t **

[英]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**. 这是我的代码,试图将字符串向量转换为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. 但是我希望我的wchar_t **保持不变,所以我想使用第三行而不是第四行。 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. 您正在分配new wchar_t[size]但将size + 1字符复制到其中。 That's undefined behavior. 那是未定义的行为。

You can convert from string to wstring like this: 您可以像这样从string转换为wstring:

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**: 但您也可以考虑传递向量,而不是int和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** . 基本上,您正在寻找的是std::vector<wchar_t*> (使用字符指针向量的几次)之一,并将其​​发送给需要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 现场例子

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

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