在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
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
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++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。