内存中的二分查找与内存中的二分查找基于磁盘的二分查找
在此程序中,我正在读取“key.pc.db”文件并打印其中间值。
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
ifstream fp;
int mval;
int sizek;
struct stat filek;
int min, max, mid;
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
sizek=sizek/sizeof(int);
min=0;
max=sizek-1;
mid=(min+max)/2;
printf("mid %d ",mid);
fp.open( "key.pc.db", ios::in | ios::binary );
fp.seekg(mid, ios::beg);
fp.read( (char *) &mval, (int) sizeof( int ) );
printf("%d mval ", mval);
getch();
return 1;
}
在此程序中,我也在读取相同的文件,但我将文件的值存储在数组中,然后打印中间值。两个程序的中间索引显示相同,但值不同。为什么会这样呢?
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
ifstream fp;
int index;
int sizek;
int kval;
struct stat filek;
int min, max, mid;
int i=0;
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
sizek=sizek/sizeof(int);
int k[sizek];
fp.open( "key.pc.db", ios::in | ios::binary );
fp.read( (char *) &kval, (int) sizeof( int ) );
while( !fp.eof() )
{
k[i++]=kval;
fp.read( (char *) &kval, (int) sizeof( int ) );
}
min=0;
max=sizek-1;
mid=(min+max)/2;
printf(" index %d ", mid);
printf(" kmid %d ", k[mid]);
getch();
return 1;
}
In this program, I am reading "key.pc.db" file and printing its mid value.
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
ifstream fp;
int mval;
int sizek;
struct stat filek;
int min, max, mid;
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
sizek=sizek/sizeof(int);
min=0;
max=sizek-1;
mid=(min+max)/2;
printf("mid %d ",mid);
fp.open( "key.pc.db", ios::in | ios::binary );
fp.seekg(mid, ios::beg);
fp.read( (char *) &mval, (int) sizeof( int ) );
printf("%d mval ", mval);
getch();
return 1;
}
In this program also I am reading the same file but I am storing the value of the file in an array and then printing the mid value. The mid index for both the program shows the same but the value comes out to be different. Why so?
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
using namespace std;
int main( int argc, char *argv[] )
{
ifstream fp;
int index;
int sizek;
int kval;
struct stat filek;
int min, max, mid;
int i=0;
if(stat("key.pc.db", &filek) ==0 )
sizek=filek.st_size;
sizek=sizek/sizeof(int);
int k[sizek];
fp.open( "key.pc.db", ios::in | ios::binary );
fp.read( (char *) &kval, (int) sizeof( int ) );
while( !fp.eof() )
{
k[i++]=kval;
fp.read( (char *) &kval, (int) sizeof( int ) );
}
min=0;
max=sizek-1;
mid=(min+max)/2;
printf(" index %d ", mid);
printf(" kmid %d ", k[mid]);
getch();
return 1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在第一种情况下,您正在寻找文件中的错误点。您应该寻找
mid*sizeof(int)
而不是mid
。You're seeking to the wrong point in the file in the first case. You should be seeking to
mid*sizeof(int)
rather thanmid
.