wcscoll() C++中的函数比较两个空结束字符串。比较基于 立法会 类别 此函数开始比较每个字符串的第一个字符。它们被视为相等,直到字符不同,或者直到到达一个表示字符串结束的空字符为止。
null
语法:
int wcscoll( const wchar_t* wcs1, const wchar_t* wcs2 )
参数: 该函数接受两个强制参数,如下所述:
- wcs1: 它是指向要比较的以null结尾的宽字符串的指针。
- wcs2: 它是指向要比较的以null结尾的宽字符串的指针。
返回值: 函数返回三个值,如下所示:
- 0,如果值指示两个字符串相等。
- 如果wcs1中不同的第一个字符大于wcs2中的相应字符,则为正值。
- 如果wcs1中不同的第一个字符小于wcs2中的相应字符,则为负值。
以下程序说明了上述功能: 项目1:
// C++ program to illustrate // wcsoll() function #include <bits/stdc++.h> using namespace std; // function to compare two strings void comparetwo( const wchar_t * wcs1, const wchar_t * wcs2) { // pointer wcs1 points string // pointer wcs2 points string_ if (wcscoll(wcs1, wcs2) < 0) wcout << wcs1 << L " precedes " << wcs2 << '' ; else if (wcscoll(wcs1, wcs2) > 0) wcout << wcs2 << L " precedes " << wcs1 << '' ; else wcout << wcs2 << L " equals " << wcs1 << '' ; } int main() { // initializing two strings wchar_t string[] = L "article" ; wchar_t string_[] = L "everyone" ; // setting american locale setlocale (LC_ALL, "en_US.utf8" ); wcout << L "In American : " ; // compare two strings with wcs1 and wcs2 comparetwo(string, string_); // setting swedish locale setlocale (LC_ALL, "sv_SE.utf8" ); wcout << L "In Swedish : " ; comparetwo(string, string_); return 0; } |
输出:
In American : article precedes everyone In Swedish : article precedes everyone
项目2:
// C++ program to illustrate // wcsoll() function #include <bits/stdc++.h> using namespace std; // function to compare two strings void comparetwo( const wchar_t * wcs1, const wchar_t * wcs2) { // pointer wcs1 points string // pointer wcs2 points string_ if (wcscoll(wcs1, wcs2) < 0) wcout << wcs1 << L " precedes " << wcs2 << '' ; else if (wcscoll(wcs1, wcs2) > 0) wcout << wcs2 << L " precedes " << wcs1 << '' ; else wcout << wcs2 << L " equals " << wcs1 << '' ; } int main() { // initializing two strings wchar_t string[] = L "geekforgeeks" ; wchar_t string_[] = L "gfg" ; // setting Czech locale setlocale (LC_COLLATE, "cs_CZ.utf8" ); wcout << L "In Czech : " ; // compare two strings with wcs1 and wcs2 comparetwo(string, string_); // setting american locale setlocale (LC_COLLATE, "en_US.utf8" ); wcout << "In the American locale: " ; comparetwo(string, string_); return 0; } |
输出:
In Czech : geekforgeeks precedes gfg In the American locale: geekforgeeks precedes gfg
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END