C++查找数据

lee_鹿游原 2011-03-21 04:41:15
查找一个数据在数组中的位置 ,并返回此数据在数组的地址..
...全文
73 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wei801516 2011-03-21
  • 打赏
  • 举报
回复
string的find找到的话 就返回与其元素相匹配的下标!~~c++primer第九章 string类型的查找详细讲解
maoxing63570 2011-03-21
  • 打赏
  • 举报
回复

#include <iostream>
#include <cstddef>
using namespace std;

int * find(int *p,const int &length,const int &value)
{
for(int i=0;i<length;++i)
{
if(p[i]==value)
{
return &p[i];
}
}

return NULL;
}

int main()
{
const size_t size=10;
int arr[size]={0};
int *p=find(arr,size,0);

if(p==NULL)
{
cout<<"Can't find the specified value in this array"<<endl;
}
else
{
cout<<p<<endl;
}

return 0;
}
pengzhixi 2011-03-21
  • 打赏
  • 举报
回复
直接返回下标就是了。然后 根据下标取址。
wei801516 2011-03-21
  • 打赏
  • 举报
回复
find

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧