C /C++中的wcsncmp()函數比較兩個寬字符串的字符。比較是按字典順序進行的。此函數采用三個參數lhs,rhs和count。它按字典順序比較了lhs和rhs的內容,最多可計數寬字符。
注意:如果lhs或rhs都不指向以null終止的寬字符串,則wcsncmp()的行為是不確定的。
用法:
int wcsncmp( const wchar_t* lhs, const wchar_t* rhs, size_t count )
參數:該函數接受三個強製性參數,如下所述:
- lhs : 要比較的字符串
- rhs : 要比較的字符串
- count : 要比較的最大字符數
返回值:該函數返回三個值,如下所示:
- 正值:如果lhs中的第一個不同字符大於rhs中的相應字符。
- 負值:如果lhs中的第一個不同字符小於rhs中的相應字符。
- 零:如果兩個字符串中比較的字符形成相同的字符串。
以下示例程序旨在說明上述函數:
示例1:
// C++ program to illustrate the
// wcsncmp() function
#include <bits/stdc++.h>
using namespace std;
// function to compare two strings
void check(wchar_t* lhs, wchar_t* rhs, int count)
{
int result;
// compare lhs and rhs by wcsncmp
result = wcsncmp(lhs, rhs, count);
// print, as per result
if (result < 0)
wcout << lhs << " precedes " << rhs << "\n";
else if (result > 0)
wcout << rhs << " precedes " << lhs << "\n";
else
wcout << L"First " << count << L" characters of "
<< L" are same"
<< "\n";
}
// Driver code
int main()
{
// initialize two strings lhs and rhs to compare
wchar_t lhs[] = L"geekforgeeks";
wchar_t rhs[] = L"geekGgeek";
// check till 4th characters and till 7th characters
check(lhs, rhs, 4);
check(lhs, rhs, 7);
return 0;
}
輸出:
First 4 characters of are same geekGgeek precedes geekforgeeks
示例2:
// C++ program to illustrate the
// wcsncmp() function
#include <bits/stdc++.h>
using namespace std;
// function to compare two strings
void check(wchar_t* lhs, wchar_t* rhs, int count)
{
int result;
// compare lhs and rhs by wcsncmp
result = wcsncmp(lhs, rhs, count);
// print, as per result
if (result < 0)
wcout << lhs << " precedes " << rhs << "\n";
else if (result > 0)
wcout << rhs << " precedes " << lhs << "\n";
else
wcout << L"First " << count << L" characters of "
<< L" are same"
<< "\n";
}
// Driver code
int main()
{
// initialize two strings lhs and rhs to compare
wchar_t lhs[] = L"01234GFG";
wchar_t rhs[] = L"01234GFG";
// check till 5th character
// and again till 8th character
check(lhs, rhs, 5);
check(lhs, rhs, 8);
return 0;
}
輸出:
First 5 characters of are same First 8 characters of are same
相關用法
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ log()用法及代碼示例
- C++ regex_iterator()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ real()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ valarray tan()用法及代碼示例
- C++ valarray pow()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSrivastava1大神的英文原創作品 wcsncmp() function in C/C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。