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


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