📜  C++中的std :: memchr

📅  最后修改于: 2021-05-30 19:38:09             🧑  作者: Mango

C++提供了各种要使用的标准模板库。其中之一是memchr()函数,它将在指定数量的字符搜索字符的第一个匹配项。
模板

const void* memchr( const void* ptr, int ch, std::size_t count );
Parameters : 
ptr : Pointer to the object to be searched for.
ch : Character to search for.
count : Number of character to be searched for.

Return value:
If the character is found, the memchr() function returns a pointer to 
the location of the character, otherwise returns null pointer.
// CPP program to illustrate memchr()
#include 
#include 
  
using namespace std;
  
int main()
{
    char sr[] = "This is a sample";
    char ch = 's';
    int count = 13;
  
    if (memchr(sr, ch, count))
        cout << ch << " is present in first "
             << count << " characters of \"" << sr << "\"";
    else
        cout << ch << " is not present in first "
             << count << " characters of \"" << sr << "\"";
  
    return 0;
}

输出:

s is present in first 13 characters of "This is a sample"

例子:

// CPP program to illustrate memchr()
#include 
#include 
  
int main()
{
    char arr[] = { 'b', 'a', 'd', 'e', 'f', 'A', 'g' };
    char* pc = (char*)std::memchr(arr, 'g', sizeof arr);
    if (pc != NULL)
        std::cout << "search character found\n";
    else
        std::cout << "search character not found\n";
}

输出:

search character found
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”