回答編集履歴

2

nits

2022/01/18 07:24

投稿

int32_t
int32_t

スコア20884

test CHANGED
@@ -3,9 +3,10 @@
3
3
  [mbstowcs_s, _mbstowcs_s_l](https://docs.microsoft.com/ja-jp/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l?view=msvc-170)
4
4
 
5
5
  ```cpp
6
- std::wstring ToWstr(std::string Str)
6
+ std::wstring ToWstr(const std::string& Str)
7
7
  {
8
8
  const char* cs = Str.c_str();
9
+ // wstring の必要サイズを求める
9
10
  size_t st;
10
11
  mbstowcs_s(&st, nullptr, 0, cs, Str.length());
11
12
 

1

コード例

2022/01/18 07:17

投稿

int32_t
int32_t

スコア20884

test CHANGED
@@ -2,3 +2,17 @@
2
2
 
3
3
  [mbstowcs_s, _mbstowcs_s_l](https://docs.microsoft.com/ja-jp/cpp/c-runtime-library/reference/mbstowcs-s-mbstowcs-s-l?view=msvc-170)
4
4
 
5
+ ```cpp
6
+ std::wstring ToWstr(std::string Str)
7
+ {
8
+ const char* cs = Str.c_str();
9
+ size_t st;
10
+ mbstowcs_s(&st, nullptr, 0, cs, Str.length());
11
+
12
+ std::wstring ws(st, 0); // 終端NULを含めたサイズを確保
13
+ mbstowcs_s(&st, &ws[0], st, cs, Str.length());
14
+ ws.resize(st - 1); // 終端NULを捨てる
15
+ return ws;
16
+ }
17
+ ```
18
+