std::mbsinit
出自 cppreference.cn
定义于头文件 <cwchar> |
||
int mbsinit( const std::mbstate_t* ps); |
||
若 ps 不是空指针,则 mbsinit
函数确定所指向的 std::mbstate_t 对象是否描述初始转换状态。
内容 |
[编辑] 注解
尽管零初始化的 std::mbstate_t 始终表示初始转换状态,但可能存在其他 std::mbstate_t 值也表示初始转换状态。
[编辑] 参数
ps | - | 指向要检查的 std::mbstate_t 对象的指针 |
[编辑] 返回值
0 若 ps 不是空指针且不表示初始转换状态,则为非零值,否则为零。
[编辑] 示例
运行此代码
#include <clocale> #include <cwchar> #include <iostream> #include <string> int main() { // allow mbrlen() to work with UTF-8 multibyte encoding std::setlocale(LC_ALL, "en_US.utf8"); // UTF-8 narrow multibyte encoding std::string str = "水"; // or u8"\u6c34" or "\xe6\xb0\xb4" std::mbstate_t mb = std::mbstate_t(); (void)std::mbrlen(&str[0], 1, &mb); if (!std::mbsinit(&mb)) std::cout << "After processing the first 1 byte of " << str << " the conversion state is not initial\n"; (void)std::mbrlen(&str[1], str.size() - 1, &mb); if (std::mbsinit(&mb)) std::cout << "After processing the remaining 2 bytes of " << str << ", the conversion state is initial conversion state\n"; }
输出
After processing the first 1 byte of 水 the conversion state is not initial After processing the remaining 2 bytes of 水, the conversion state is initial conversion state
[编辑] 参见
迭代多字节字符串所需的转换状态信息 (类) | |