调试断言在使用 ispunct('ø') 时失败

Debug Assertion Failed when using ispunct('ø')

本文关键字:失败 断言 ispunct 调试      更新时间:2023-10-16

我正在编写一个处理大部分文本的程序,需要删除标点符号。我遇到了调试断言失败错误,并将其隔离为此:它在非英语字母上测试 ispunct(( 时发生。

我的测试程序现在是这样的:

主.c

int main() {
    ispunct('ø');
    cin.get();
    return 0;
}

"调试断言失败"窗口如下所示:错误的屏幕截图

我尝试过的所有非英语字母都会导致此问题,包括"æ","ø","å","é"等。这可能是我忽略的非常简单的事情,所以我感谢任何帮助!

字符'ø'必须表示为unsigned char,否则应使用类型wchar_tstd::ispunct,例如:

#include <iostream>
#include <locale>
int main()
{
    const wchar_t c = L'ø';
    std::locale loc("en_US.UTF-8");
    std::ispunct(c, loc);
}

对于您的问题,您还可以执行以下操作:

#include <locale>
#include <string>
#include <algorithm>
#include <functional>
int main()
{
    std::wstring word = L"søme.?.thing";
    std::locale loc("en_US.UTF-8");
    using namespace std::placeholders;
    word.erase(std::remove_if(word.begin(), word.end(),
           std::bind(std::ispunct<wchar_t>, _1, loc)), word.end());
    std::wcout << word << std::endl;
}