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


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

C++ 中的wcslen() 函數返回給定寬字符串的長度。

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

wcslen()原型

size_t wcslen( const wchar_t* str );

wcslen() 以空終止的寬字符串 str 作為其參數並返回其長度。長度不包括空寬字符。如果寬字符串中沒有空寬字符,則函數的行為未定義。

參數:

  • str :指向要計算其長度的空終止寬字符串的指針。

返回:

  • wcslen() 函數返回空終止的寬字符串的長度。

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

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

int main()
{
	setlocale(LC_ALL, "en_US.utf8");
	
	wchar_t str1[] = L"Hello World\u0021";
	wchar_t str2[] = L"\u0764\u077a\u077c\u079f\u07a1\u072e";
	int len1 = wcslen(str1);
	int len2 = wcslen(str2);
	
	cout << "Length of str1 = " << len1 << endl;
	cout << "Length of str2 = " << len2 << endl;
	if (len1 > len2)
		cout << "str1 is longer than str2";
	else if (len1 < len2)
		cout << "str2 is longer than str1";
	else
		cout << "str1 and str2 are of equal length";
	
	return 0;
}

運行程序時,輸出將是:

Length of str1 = 12
Length of str2 = 6
str1 is longer than str2

相關用法


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