趣谈C++之memchr

先看一下官方做出的解释

const void * memchr ( const void * ptr, int value, size_t num );
      void * memchr (       void * ptr, int value, size_t num );
Locate character in block of memory
Searches within the first  num bytes of the block of memory pointed by  ptr for the first occurrence of  value (interpreted as an  unsigned char), and returns a pointer to it.

Both  value and each of the bytes checked on the the  ptr array are interpreted as  unsigned char for the comparison.

Parameters

ptr
Pointer to the block of memory where the search is performed.
value
Value to be located. The value is passed as an  int, but the function performs a byte per byte search using the unsigned char conversion of this value.
num
Number of bytes to be analyzed.
size_t is an unsigned integral type.

Return Value

A pointer to the first occurrence of  value in the block of memory pointed by  ptr.
If the  value is not found, the function returns a null pointer.

Portability

In C, this function is only declared as:

void * memchr ( const void *, int, size_t ); 

instead of the two overloaded versions provided in C++.

简单粗暴的说,这个函数可以帮助我们寻找一段字符串中是否有我们所需要的某个字符。

接下来讲讲如何使用这个函数以及一些注意点。


参数:(const void* ptr ,int value,size_t  num)

ptr:指针,这个指针指向我们要选择的开始查找的起始位置

value:所要查找的字符,虽然写的int 类型但一般是直接传一个字符:比如‘A’,‘R’等等

num:需要分析的字节数(可以简单的理解为查找的次数)


返回值:void *

如果找到字符,则返回的是一个指针,这个指针指向字符数组中该字符;

如果没有找到符合要求的字符,则返回NULL;

下面看个例子:
#include <iostream>
#include<string.h>
using namespace std;
int main() 

char a[]="I love WUXI";
char *p;
p=(char *)memchr(a,'X',strlen(a));
if(p!=NULL){
cout<<"找到了X它是第"<<p-a+1<<"个字符" ;
}
else cout<<"没有找到X" ;
return 0;

}


注意一点是:p=(char *)memchr(a,'X',strlen(a));

因为memchr返回的是void*,需要强制转换一下指针类型。


最后贴一个memchr的源码:



/***
*memchr.c - search block of memory for a given character
*
*       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
*       defines memchr() - search memory until a character is
*       found or a limit is reached.
*
*******************************************************************************/


#include <cruntime.h>
#include <string.h>


/***
*char *memchr(buf, chr, cnt) - search memory for given character.
*
*Purpose:
*       Searches at buf for the given character, stopping when chr is
*       first found or cnt bytes have been searched through.
*
*Entry:
*       void *buf  - memory buffer to be searched
*       int chr    - character to search for
*       size_t cnt - max number of bytes to search
*
*Exit:
*       returns pointer to first occurence of chr in buf
*       returns NULL if chr not found in the first cnt bytes
*
*Exceptions:
*
*******************************************************************************/


void * __cdecl memchr (
        const void * buf,
        int chr,
        size_t cnt
        )
{
        while ( cnt && (*(unsigned char *)buf != (unsigned char)chr) ) {
                buf = (unsigned char *)buf + 1;
                cnt--;
        }


        return(cnt ? (void *)buf : NULL);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值