解决方案可能很明显,但我没有看到。我有这个简单的 C++ 代码:
// Build the search pattern
// sPath is passed in as a parameter into this function
trim_right_if(sPath, is_any_of(L"\\"));
wstring sSearchPattern = sPath + L"\\*.*";
我的问题是 + 运算符无效(已在调试器中检查)。字符串 sSearchPattern
仅初始化为 sPath
的值。
注意:sPath
是一个wstring
。
我想要实现的示例:
sSearchPattern -> C:\SomePath\*.*
更多信息:
当我在调试器中查看 sPath 时,我在最后一个字符后看到两个 NULL 字符。当我查看 sSearchPattern 时,附加了“\*.*”,但在两个 NULL 字符之后。有什么解释吗?
最佳答案
这应该有效,并且确实适用于 VS2010,SP1:
#include <iostream>
#include <string>
int main()
{
const std::wstring sPath = L"C:\\SomePath";
const std::wstring sSearchPattern = sPath + L"\\*.*";
std::wcout << sSearchPattern << L'\n';
return 0;
}
这打印
C:\SomePath\*.*
对我来说。
关于c++ - std::wstring:与 + 的连接无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787198/