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


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

C++ 中的wmemchr() 函數在指定數量的寬字符中搜索第一次出現的寬字符。

wmemchr() 函數在<cwchar> 頭文件中定義。

wmemchr()原型

const wchar_t* wmemchr( const wchar_t* ptr, wchar_t ch, size_t count );
wchar_t* wmemchr( wchar_t* ptr, wchar_t ch, size_t count );

wmemchr() 函數采用三個參數:ptr , chcount。它在 ptr 指向的對象的第一個 count 寬字符中定位第一次出現的 ch

如果 count 的值為零,則該函數返回一個空指針。

參數:

  • ptr:指向要搜索的寬字符數組的指針。
  • ch:要搜索的寬字符。
  • count:要搜索的寬字符數。

返回:

如果找到字符,wmemchr() 函數返回指向寬字符位置的指針,否則返回空指針。

示例:wmemchr() 函數如何工作?

#include <cwchar>
#include <clocale>
#include <iostream>
using namespace std;

int main()
{
	setlocale(LC_ALL, "en_US.utf8");

	wchar_t ptr[] = L"\u0102\u0106\u0126\u01f6\u021c\u0246\u0376\u024a";
	wchar_t ch = L'Ħ';
	int count = 5;
	
	if (wmemchr(ptr,ch, count))
		wcout << ch << L" is present in first " << count << L" characters of \"" << ptr << "\"";
	else
		wcout << ch << L" is not present in first " << count << L" characters of \"" << ptr << "\"";
	
	return 0;
}

運行程序時,輸出將是:

Ħ is present in first 5 characters of "ĂĆĦǶȜɆͶɊ"

相關用法


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