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


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

C++中的wcschr()函數在寬字符串中搜索寬字符的首次出現。終止的null寬字符被視為字符串的一部分。因此,還可以定位它以便檢索指向寬字符串末尾的指針。

用法:

const wchar_t* wcschr (const wchar_t* ws, wchar_t wc)
      wchar_t* wcschr (      wchar_t* ws, wchar_t wc)

參數:該函數接受兩個強製性參數,如下所述:


  1. ws:指向要搜索的空終止的寬字符串的指針
  2. wc:要定位的寬字符

返回值:該函數返回兩個值,如下所示:

  1. 指向字符串中字符首次出現的指針。
  2. 如果找不到字符,則函數返回空指針。

以下示例程序旨在說明上述函數:
程序1:

// C++ program to illustrate 
// wcschr() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize wide string 
    wchar_t ws[] = L"This is some good coding"; 
  
    wchar_t* point; 
    wprintf(L"Looking for the 'o' character in \"%ls\"...\n", ws); 
  
    // initialize the search character 
    point = wcschr(ws, L'o'); 
  
    // search the place and print 
    while (point != NULL) { 
        wprintf(L"found at %d\n", point - ws + 1); 
        point = wcschr(point + 1, L'o'); 
    } 
  
    return 0; 
}
輸出:
Looking for the 'o' character in "This is some good coding"...
found at 10
found at 15
found at 16
found at 20

程序2:

// C++ program to illustrate 
// wcschr() function 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    // initialize wide string 
    wchar_t ws[] = L"geekforgeeks"; 
  
    wchar_t* point; 
    wprintf(L"Looking for the 'g' character in \"%ls\"...\n", ws); 
  
    // initialize the search character 
    point = wcschr(ws, L'g'); 
  
    // search the place and print 
    while (point != NULL) { 
        wprintf(L"found at %d\n", point - ws + 1); 
        point = wcschr(point + 1, L'g'); 
    } 
  
    return 0; 
}
輸出:
Looking for the 'g' character in "geekforgeeks"...
found at 1
found at 8


相關用法


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