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


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

C /C++中的wmemchr()函數在寬字符塊中定位字符。此函數在ptr指向的塊的前num個字符內搜索ch的首次出現,並返回指向它的指針。

用法:

const wchar_t* wmemchr( const wchar_t* ptr, wchar_t ch, size_t num )
or
wchar_t* wmemchr( wchar_t* ptr, wchar_t ch, size_t num )



參數:該函數接受三個強製性參數,如下所述:

  • ptr: 指定指向要搜索的字符數組的指針。
  • ch: 指定要定位的字符。
  • num: 指定要比較的wchar_t類型的元素數。

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

  • 成功時,它將返回一個指向字符數組位置的指針。
  • 否則,返回NULL指針。

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

// C++ program to illustarte 
// wmemchr() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize the string to be scanned 
    wchar_t ptr[] = L"GeeksForGeeks"; 
  
    // character to be searched for 
    wchar_t ch = L'o'; 
  
    // length, till the character to be search for is 8 
    // run the function to check if the character is present 
    bool look = wmemchr(ptr, ch, 8); 
    if (look) 
        wcout << "'" << ch << "'" << L" is present in first "
              << 8 << L" characters of \"" << ptr << "\""; 
    else
        wcout << "'" << ch << "'" << L" is not present in first "
              << 8 << L" characters of \"" << ptr << "\""; 
  
    return 0; 
}
輸出:
'o' is present in first 8 characters of "GeeksForGeeks"

示例2:

// C++ program to illustarte 
// wmemchr() function 
  
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize the string to be scanned 
    wchar_t ptr[] = L"GFG"; 
  
    // character to be searched for 
    wchar_t ch = L'p'; 
  
    // length, till the character to be search for is 3 
    // run the function to check if the character is present 
    bool look = wmemchr(ptr, ch, 3); 
    if (look) 
        wcout << "'" << ch << "'" << L" is present in first "
              << 3 << L" characters of \"" << ptr << "\""; 
    else
        wcout << "'" << ch << "'" << L" is not present in first "
              << 3 << L" characters of \"" << ptr << "\""; 
  
    return 0; 
}
輸出:
'p' is not present in first 3 characters of "GFG"


相關用法


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