Namespaces
Variants

std:: mbsinit

From cppreference.net
ヘッダーで定義 <cwchar>
int mbsinit ( const std:: mbstate_t * ps ) ;

ps が null ポインタでない場合、 mbsinit 関数は、指し示された std::mbstate_t オブジェクトが初期変換状態を表しているかどうかを判定します。

目次

注記

ゼロ初期化された std::mbstate_t は常に初期変換状態を表しますが、 std::mbstate_t の他の値も初期変換状態を表す場合があります。

パラメータ

ps - 検査対象の std::mbstate_t オブジェクトへのポインタ

戻り値

0 psがnullポインタではなく、かつ初期変換状態を表していない場合、それ以外の場合は非ゼロ値。

#include <clocale>
#include <cwchar>
#include <iostream>
#include <string>
int main()
{
    // mbrlen()がUTF-8マルチバイトエンコーディングで動作するように設定
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8ナローマルチバイトエンコーディング
    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

関連項目

マルチバイト文字列を反復処理するために必要な変換状態情報
(クラス)