命名空间
变体
操作

std::mbsinit

来自 cppreference.cn
< cpp‎ | string‎ | multibyte
在头文件 <cwchar> 中定义
int mbsinit( const std::mbstate_t* ps);

如果 ps 不是空指针,则 mbsinit 函数确定指向的 std::mbstate_t 对象是否描述了初始转换状态。

目录

[编辑] 注意

尽管零初始化的 std::mbstate_t 总是表示初始转换状态,但 std::mbstate_t 可能还有其他值也表示初始转换状态。

[编辑] 参数

ps - 指向要检查的 std::mbstate_t 对象的指针

[编辑] 返回值

如果 ps 不是空指针且不表示初始转换状态,则返回 0,否则返回非零值。

[编辑] 示例

#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

[编辑] 另请参阅

迭代多字节字符串所需的转换状态信息
(类) [编辑]
C 文档 中关于 mbsinit 的内容