我有一个以竖线分隔的字符串,我想将其放入名为result
的向量中。但是,它不能在getline
上编译。如果我删除getline
中的管道分隔符,那么它将编译:
#include <sstream>
using namespace std;
wstringstream ss(L"1,2,3|4,5,6|7,8,9|");
vector<wstring> result;
wstring substr;
while (ss.good())
{
getline(ss, substr, '|'); // <- this does not compile with wchar_t
result.push_back(substr);
}
如何对传入的wchar_t
字符串使用getline
?我可以使用WideCharToMultiByte
,但是如果我可以使用带有wchar_t
的getline
,那么就需要进行大量的处理。
发布于 2021-01-23 15:39:59
您的代码无法编译,因为getline
要求分隔符和流使用相同的字符类型。字符串流ss
使用wchar_t
,但是编译器将'|'
作为char
进行计算。
解决方案是使用适当的character literal,如下所示:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
wstringstream ss(L"1,2,3|4,5,6|7,8,9|");
wstring substr;
while (ss.good())
{
getline(ss, substr, L'|');
std::wcout << substr << std::endl;
}
}
https://stackoverflow.com/questions/65493128
复制相似问题