time()函的问题

熊猫过隙 2009-04-08 05:22:47
IDE:VC++6.0


#include<iostream.h>
#include<time.h>

int seqsearch(int a[],int n,int x)
{
int i = 0;

while (i<n && a[i]!=x)
{
i++;
}
if (i==n) return -1;
}

main()
{
int a[10] = {12,23,52,75,48,25,36,29,65,47};
int i,k;
double start,stop;



time(&start);
k = seqsearch(a,10,65);
time(&stop);
double runTime = stop -start;

cout << " "<< 10 << " " << runTime << endl;

for(i=0; i<10; i++)
{
cout << a[i] << " ";
}
cout << endl;

return 0;
}


调试信息
--------------------Configuration: SeqSearch - Win32 Debug--------------------
Compiling...
SeqSearcg.cpp
D:\alexa\VC\SeqSearch\SeqSearcg.cpp(23) : error C2664: 'time' : cannot convert parameter 1 from 'double *' to 'long *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
D:\alexa\VC\SeqSearch\SeqSearcg.cpp(25) : error C2664: 'time' : cannot convert parameter 1 from 'double *' to 'long *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
执行 cl.exe 时出错.

SeqSearch.exe - 1 error(s), 0 warning(s)


多谢!
...全文
96 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
直接用time_t来声明就可以了。
old-six-programmer 2009-04-08
  • 打赏
  • 举报
回复
因为time返回返回从公元1970年1月1日的UTC时间从0时0分0秒算起到现在所经过的秒数
显然时间粒度太大

可以使用gettimeofday
可以精确到微妙

[Quote=引用 8 楼 xhz_panda 的回复:]
试过改成long.
虽然没有错误了能运行程序了.
但是结果是0,显然不对.

二楼的改法,运行的结果和你建议的改法的结果是一样的.
[/Quote]
熊猫过隙 2009-04-08
  • 打赏
  • 举报
回复
试过改成long.
虽然没有错误了能运行程序了.
但是结果是0,显然不对.

二楼的改法,运行的结果和你建议的改法的结果是一样的.
mengde007 2009-04-08
  • 打赏
  • 举报
回复
调试信息很清楚……
liliangbao 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 donle1000 的回复:]
LZ是不是要表达这个意思?


C/C++ code
#include<iostream.h>
#include<time.h>

int seqsearch(int a[],int n,int x)
{
int i = 0;

while (i<n && a[i]!=x)
{
i++;
}
if (i==n) return -1;
}

main()
{
int a[10] = {12,23,52,75,48,25,36,29,65,47};
int i,k;
clock_t start,stop;



start = clock();
k = seqsearch(a,10,65);

[/Quote]
正解~
time_t time(long *tloc); 因此参数使用错了~
ylywyn136 2009-04-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zmlovelx 的回复:]
double start,stop;

改成long
[/Quote]..
帅得不敢出门 2009-04-08
  • 打赏
  • 举报
回复
time_t time(
time_t *timer
);
帅得不敢出门 2009-04-08
  • 打赏
  • 举报
回复
一般用time_t
donle1000 2009-04-08
  • 打赏
  • 举报
回复
LZ是不是要表达这个意思?


#include<iostream.h>
#include<time.h>

int seqsearch(int a[],int n,int x)
{
int i = 0;

while (i<n && a[i]!=x)
{
i++;
}
if (i==n) return -1;
}

main()
{
int a[10] = {12,23,52,75,48,25,36,29,65,47};
int i,k;
clock_t start,stop;



start = clock();
k = seqsearch(a,10,65);
stop = clock();
clock_t runTime = stop -start;

cout << " "<< 10 << " " << runTime << endl;

for(i=0; i<10; i++)
{
cout << a[i] << " ";
}
cout << endl;

return 0;
}

帅得不敢出门 2009-04-08
  • 打赏
  • 举报
回复
double start,stop;

改成long

65,211

社区成员

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

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