gpt4 book ai didi

c++ - 如何连接 2 个 LPOLESTR

转载 作者:搜寻专家 更新时间:2023-10-31 00:22:49 25 4
gpt4 key购买 nike

我想在 C++ 中连接 2 个字符串,我不能使用 char*。

我尝试了以下但不起作用:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPOLESTR o = OLESTR(s);

我需要一个将 s1 和 s2 连接起来的字符串。任何信息或网站可以对此进行更多解释?谢谢。

最佳答案

OLESTR("s")L"s" 相同(并且 OLESTR(s)Ls), 这显然不是你想要的。
使用这个:

#define url L"http://domain.com"
wstring s1 = url;
wstring s2 = L"/page.html";
wstring s = s1 + s2;
LPCOLESTR o = s.c_str();

这为您提供了一个LPCOLESTR(即一个const LPOLESTR)。如果你真的需要它是非常量的,你可以将它复制到一个新的字符串:

...
wstring s = s1 + s2;
LPOLESTR o = new wchar_t[s.length() + 1];
wcscpy(o, s.c_str()); //wide-string equivalent of strcpy is wcscpy
//Don't forget to delete o!

或者,为了完全避免 wstring(不推荐;最好将您的应用程序转换为在任何地方都使用 wstring,而不是使用 LPOLESTR的):

#define url L"http://domain.com"
LPCOLESTR s1 = url;
LPCOLESTR s2 = L"/page.html";
LPOLESTR s = new wchar_t[wcslen(s1) + wcslen(s2) + 1];
wcscpy(s, s1); //wide-string equivalent of strcpy is wcscpy
wcscat(s, s2); //wide-string equivalent of strcat is wcscat
//Don't forget to delete s!

关于c++ - 如何连接 2 个 LPOLESTR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2914202/

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