当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。