【问题标题】:How can I use a wregex in C++11?如何在 C++11 中使用 wregex?
【发布时间】:2013-07-20 03:10:12
【问题描述】:

我尝试使用普通的正则表达式,一切都很好,当我切换到 wregex 在 Unicode 字符中搜索和标记时,它失败了,我不明白为什么。
有人可以指出我在这里缺少什么吗?

map<string, int> container;
wifstream file("ftest.txt"); 
wregex reg(_T("\\w+"));
wstring s=_T("");
while (file.good())
{
    file>>s;
    for ( wsregex_iterator it (s.cbegin(), s.cend(), reg),it_end; it != it_end; ++it)
    {
        container[(*it)[0]]++ ;
    }

}

我的文件内容是波斯语,如:

بسم الله الرحمن الرحیم 
تست یک تست 2 . 2357 نفر آمار تست اولیه هرچی!!

这些是它产生的错误:

error C2679: binary '[' : no operator found which take a right-hand 'const std::sub_match<_bidit>' 类型的操作数(或者没有 可接受的转换)

IntelliSense:没有运算符“[]”与这些操作数的操作数类型匹配 是:std::map, 标准::分配器>> [常量 std::sub_match>>> ]

【问题讨论】:

  • 它是如何失败的?你能从你的非英语文件和你期望的输出中给出一个示例行吗?
  • 它在编译时停止了!我会在几秒钟内将错误添加到问题中
  • 您使用的是哪个编译器和版本?在哪个平台下?
  • Visual C++2012 UpdatePack3
  • 这就是你如何使用 wstring 和 ifstream stackoverflow.com/questions/5382451/…

标签: regex visual-c++ c++11


【解决方案1】:

s.begin() 和 s.end() 应该返回 wstring 迭代器(使 s 成为 wstring)。

这就是你如何在 ifstream 中使用 wstring How to use std::ifstream to read in a binary file with a wide string path

地图也应该是map&lt;wstring, int&gt;

【讨论】:

  • 谢谢,但是当我尝试将 cout 与我的地图一起使用时出现错误!? cout
  • 您可以使用 ifstream 技巧,但反过来。使用 cout
  • 或者更好地使用 std::wcout :)
猜你喜欢
  • 2019-02-24
  • 1970-01-01
  • 2013-07-05
  • 1970-01-01
  • 2012-05-11
  • 1970-01-01
  • 2013-07-20
  • 2012-12-20
相关资源
最近更新 更多