當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ mbsinit()用法及代碼示例


mbsinit()是C++中的內置函數,用於檢查ps(作為此函數的參數傳遞)是否指向描述初始轉換狀態的mbstate_t對象。對於表示初始狀態或ps是空指針的任何mbstate_t對象,此函數返回非零值。

可以通過調用以下命令將ps指向的狀態設置為初始狀態:

// ps points now to a zero-valued object
memset (ps, 0, sizeof(*ps));

用法:


int mbsinit( const mbstate_t* ps)

參數:該函數接受一個參數,如下所述:

  • ps :指向mbstate_t對象的指針

返回值:該函數返回兩個值,如下所示:

  • 如果ps不是空指針並且不代表初始轉換狀態,則返回0。
  • 如果ps是空指針或代表初始轉換狀態,則為非零值。

以下示例程序旨在說明上述函數:
示例1:

// C++ program to illustrate 
// mbsinit() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
// function to check if the object describes 
// the initial conversion state 
void checkfor(mbstate_t ps) 
{ 
    // (mbstate_t) Type that holds the information necessary 
    // to maintain the state when converting between 
    // sequences of multibyte characters and 
    // wide characters (either way). 
  
    int func = mbsinit(&ps); 
  
    if (func) 
        cout<<"The conversion state is initial"
                                 <<" conversion state\n"; 
  
    else
        cout<<"The conversion state is not initial"
                                    <<" conversion state"; 
} 
  
// Driver code 
int main() 
{ 
    setlocale(LC_ALL, "en_US.utf8"); 
  
    // initializing the string 
    char str[] = "\u00df"; 
  
    // initial state 
    mbstate_t ps = mbstate_t(); 
  
    // check if ps is liknked to 
    // initial conversion state 
    checkfor(ps); 
  
    mbrlen(str, 1, &ps); 
  
    // check if, after geting the 
    // length of multibyte character 
    checkfor(ps); 
  
    return 0; 
}
輸出:
The conversion state is initial conversion state
The conversion state is not initial conversion state

示例2:

// C++ program to illustrate 
// mbsinit() function 
// with empty string 
  
#include <bits/stdc++.h> 
using namespace std; 
  
// function to chack if 
// object describes the initial conversion state 
void checkfor(mbstate_t ps) 
{ 
    // (mbstate_t) Type that holds the information necessary 
    // to maintain the state when converting between 
    // sequences of multibyte characters and 
    // wide characters (either way). 
  
    int func = mbsinit(&ps); 
  
    if (func) 
        cout << "the conversion state is initial"
                                <<" conversion state\n"; 
  
    else
        cout << "the conversion state is not initial"
                                <<" conversion state"; 
} 
  
// Driver code 
int main() 
{ 
    setlocale(LC_ALL, "en_US.utf8"); 
  
    // initializing the string 
    char str[] = ""; 
  
    // initial state 
    mbstate_t ps = mbstate_t(); 
  
    // check if ps is liknked to 
    // initial conversion state 
    cout << "After ps is initialized, "; 
    checkfor(ps); 
  
    mbrlen(str, 0, &ps); 
  
    // check if, after geting the 
    // length of multibyte character 
    cout << "After performing some task, "; 
    checkfor(ps); 
  
    return 0; 
}
輸出:
After ps is initialized, the conversion state is initial conversion state
After performing some task, the conversion state is initial conversion state


相關用法


注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 mbsinit() Function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。