gpt4 book ai didi

d - 如何在 D 2.0 中使用 wchar** 初始化 wstring[]

转载 作者:行者123 更新时间:2023-12-01 21:48:04 25 4
gpt4 key购买 nike

在 C++ 中,我可以使用 wchar_t** 初始化向量 ,如下例所示:

#include <windows.h>
#include <string>
#include <vector>
#include <cwchar>
using namespace std;

int main() {
int argc;
wchar_t** const args = CommandLineToArgvW(GetCommandLineW(), &argc);
if (args) {
const vector<wstring> argv(args, args + argc);
LocalFree(args);
}
}

但是,有没有办法在 D 2.0 中用 wchar** 初始化 wstring[] ?

我可以这样将 wchar** 的内容添加到 wstring[] 中:

import std.c.windows.windows;
import std.c.wcharh;

extern(Windows) {
wchar* GetCommandLineW();
wchar** CommandLineToArgvW(wchar*, int*);
void* LocalFree(void*);
}

void main() {
int argc;
wchar** args = CommandLineToArgvW(GetCommandLineW(), &argc);
if (args) {
wstring[] argv;
for (size_t i = 0; i < argc; ++i) {
wstring temp;
const size_t len = wcslen(args[i]);
for (size_t z = 0; z < len; ++z) {
temp ~= args[i][z];
}
argv ~= temp;
}
LocalFree(args);
}
}

但是,我想找到一种更干净、更简单的方法,例如 C++ 版本。 (性能不是问题)

最佳答案

这是使用切片的更简单的版本:

import std.c.windows.windows;
import std.c.wcharh;
import std.conv;

extern(Windows) {
wchar* GetCommandLineW();
wchar** CommandLineToArgvW(wchar*, int*);
void* LocalFree(void*);
}

void main() {
int argc;
wchar** args = CommandLineToArgvW(GetCommandLineW(), &argc);
if (args) {
wstring[] argv = new wstring[argc];
foreach (i, ref arg; argv)
arg = to!wstring(args[i][0 .. wcslen(args[i])]);
LocalFree(args);
}
}

另一种选择是使用 void main(string[] args) 并根据需要转换为 args wstring。

关于d - 如何在 D 2.0 中使用 wchar** 初始化 wstring[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6119715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com