哪个地方错了?

flycat1 2002-06-13 11:24:21
请各位大侠帮我看看哪个地方错了:
#include <iostream>
#include <vector>
using namespace std;

int* find(const vector<int> &vec,int value)
{
for(int ix=0;ix<vec.size();++ix)
if(vec[ix]==value)
return &vec[ix];
return 0;
}

int main()
{
int array[]={12,123,4,56,7,127,80,9};
vector<int> vec(array,array+8);
int *p;
int value(123);
*p=find(vec,value);
cout<<*p<<endl;
return 0;
}
...全文
52 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
he_qiancsdn 2002-06-13
  • 打赏
  • 举报
回复
你既然在find 中声明vector为const型,当然你返回值也要是const int * 的类型了,同时你调用该函数返回的值也要定义成const int * 型的。
#include <iostream>
#include <vector>
using namespace std;

const int* find(const vector<int> &vec,int value)
{
for(int ix=0;ix<vec.size();++ix)
if(vec[ix]==value)
return &vec[ix];
return 0;
}

int main()
{
int array[]={12,123,4,56,7,127,80,9};
vector<int> vec(array,array+8);
const int *p;
int value(123);
p=find(vec,value);
cout<<*p<<endl;
return 0;
}

或者你也可以去掉find参数声明中的const
#include <iostream>
#include <vector>
using namespace std;

int* find(vector<int> &vec,int value)
{
for(int ix=0;ix<vec.size();++ix)
if(vec[ix]==value)
return &vec[ix];
return 0;
}

int main()
{
int array[]={12,123,4,56,7,127,80,9};
vector<int> vec(array,array+8);
int *p;
int value(123);
p=find(vec,value);
cout<<*p<<endl;
return 0;
}
这两种方法我都已经运行过了,没有问题。
mickey_gxj 2002-06-13
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
using namespace std;

const int* find(const vector<int> &vec,int value)
{
for(int ix=0;ix<vec.size();++ix)
if(vec[ix]==value)
return &vec[ix];
return 0;
}

int main()
{
int array[]={12,123,4,56,7,127,80,9};
vector<int> vec(array,array+8);
const int *p;
int value(123);
p=find(vec,value);
cout<<*p<<endl;
return 0;
}

69,382

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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