📜  C / C++中的mbsinit()函数

📅  最后修改于: 2021-05-30 14:20:14             🧑  作者: Mango

mbsinit()是C++中的内置函数,用于检查ps (作为此函数的参数传递)是否指向描述初始转换状态的mbstate_t对象。对于表示初始状态或ps是空指针的任何mbstate_t对象,此函数返回非零值。

可以通过调用ps将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 
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 
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
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”