- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个单词流,在循环的每次运行中给我一个单词作为 std::string
。但理想情况下,这应该是 std::wstring
。因此,在获得字符串后,我将其转换为 std::wstring
。这是我输入到 std:wstringstream
中的。最后,在处理完流中的所有单词后,我将 std:wstringstream
转换为 std::wstring
,然后搜索所需的术语(最初是std::wstring
) 在其中。这是我的代码:
while (stream)
{
std::string word = stream->getWord();
boost::trim(word);
std::wstring longWord(word.length(), L' '); // Make room for characters
std::copy(word.begin(), word.end(), longWord.begin());
fMyWideCharStream << longWord;
stream->next();
}
std::wstring fContentString = fMyWideCharStream.str();
size_t nPos = fContentString.find(fSearchString, 0); //fSearchString is std::wstring
while(nPos != std::wstring::npos)
{
qDebug() << "Pos: " << nPos << endl;
nPos = fContentString.find(fSearchString, nPos+1);
}
我有这个字符串:Passive Aggressive Dealing With Passive Aggression, Lost Happiness & Disconnection 版权所有 © 2014,其中 © 是一个宽字符。作为 std::string
它占据了两个位置。作为 std::wstring
它需要 1,这正是我想要的。但是,在尝试使用值为 L"2014"
的 fSearchString
时,我得到的值仍然是 96,而它应该是 95,因为这个字符串现在是 std::wstring
.
知道我应该怎么做才能解决这个问题吗?
最佳答案
因为原始的 string
不是纯 ASCII - 它包含多字节字符 '©',所以从 string
转换为 wstring
是错误的> 使用逐字符转换。因此两者
std::wstring longWord(word.length(), L' '); // Make room for characters
std::copy(word.begin(), word.end(), longWord.begin());
和
std::wstring longWord(word.begin(), word.end());
不适用于包含多字节字符的 string
。要在 Windows 上将多字节字符 string
正确转换为 wstring
,您可以使用 mbstowcs(): http://www.cplusplus.com/reference/cstdlib/mbstowcs/
以独立于平台的方式,使用 C++11(将选项编译为 clang:-std=c++1
),您可以这样做:https://stackoverflow.com/a/14809553/1915854 , https://stackoverflow.com/a/18597384/1915854
如果您需要超出单个 wchar_t 可以存储的字符的示例:
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring longWord = converter.from_bytes(word);
如果您不需要超出单个 wchar_t 可以存储的字符:
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring longWord = converter.from_bytes(word);
必要的包括:
#include <locale>
#include <codecvt>
#include <string>
在 Boost 中,C++11 之前似乎还有其他选项。
关于c++ - 如何搜索从 wstringstream 获得的宽字符串中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31169355/
我正在尝试将 3 字节 double 型转换为字符串。以下是我的代码。 double b = 0xFFFFFF; std::wstring ss; std::wstringstream
我在使用 wstringstream 时遇到问题。当我这样做的时候 std::wstringstream ss; wchar_t* str = NULL; ss << str;
我有一个 wstringstream: wstringstream sstream; AlterSstream(sstream); wstringstream bar_stream; bar_stre
我有一个单词流,在循环的每次运行中给我一个单词作为 std::string。但理想情况下,这应该是 std::wstring。因此,在获得字符串后,我将其转换为 std::wstring。这是我输入到
我有一个 std::wstringstream,我在我的类中将其用作某种缓冲区,并且该类中的大部分方法都使用了它。但是,当我尝试做这样的事情时: #include class foo { pub
我正在使用一个开源代码,他们创建了一个新的字符串类,它们的语法如下: OpenEvent(EVENT_ALL_ACCESS, FALSE, String() << L"SOMETEXT" <<
我已经使用 wstringstream 构建了一个字符串,需要将其分配给类型为 LPWSTR 的 struct 成员。我尝试使用 my_stringstream.str().c_str() 但出现以下
我尝试执行以下操作: wstring wStringStreamToWString(wstringstream uStream) { wstring s(uStream.str()); r
出于某种原因,当我尝试创建一个 wstringstream 时,我的项目无法编译: std::wstringstream stringstream; 这会导致错误 C2079: 'stringstre
我是 Winapi 的初学者,我正在尝试将 wstringstream 转换为 LPCWSTR,如下所示(在 WM_PAINT 内): wstringstream ws; ws (ws.str().l
wstring ss; ss basic_ostream& operator& os, const char* s
这是对问题的跟进和回答 here .我想使用文本文件作为资源,然后将其作为 stringstream 加载,以便我可以解析它。 以下代码显示了我目前拥有的内容: std::string filenam
如果我有一个 wstringstream,并且我想获取它的 .str() 数据作为 LPCWSTR,我该怎么做? 最佳答案 你可以做 wstringstream.str().c_str() as De
我有一个 WCHAR[]、一个 wstringstream 和一个要复制到 WCHAR[] 的任意部分字符串流。执行此操作的最佳方法是什么? 看来一定有比这更好的方法了: for (int i = s
我正在尝试为我编写的程序添加 Unicode 支持。我的 ASCII 代码已编译并具有以下几行: std::stringstream stream("abc"); std::istream_itera
我是一名优秀的程序员,十分优秀!