两个大文件找出相同的一条记录
生活随笔
收集整理的這篇文章主要介紹了
两个大文件找出相同的一条记录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stdafx.h"
#include <iostream>
#pragma warning(disable : 4786)//這句話的位置必須是這里
#include<set>
#include <string>
#include <algorithm>
#include <time.h>
#include <windows.h>
using namespace std;
#define WORDSINLINE 150 //每行字節數
#ifdef _DEBUG
#define MOSHI "Debug模式下"
#else
#define MOSHI "Release模式下"
#endif
//定義一個仿函數用以實現比較方法
struct compare
{
?? ?bool operator()(const char* s1,const char* s2) const
?? ?{
?? ??? ?//bool b = strcmp(s1, s2) > 0;
?? ??? ?return (strcmp(s1, s2) < 0);//如果把b直接寫成false,則set中無論insert多少次只有一條數據。
?? ??? ??? ??? ? //如果把b直接寫成true,則系統不會進行排序,采用頭插方式。
?? ??? ??? ??? ? //總結:必須是兩者的表達式
?? ?}
};
int main(int argc, char* argv[])
{
?? ?//char Buffer[WORDSINLINE];
?? ?char *Buffer;
?? ?set<char*,compare>::iterator iter;
?? ?set<char*,compare> MySet1;
?? ?set<char*,compare> MySet2;
?? ?//打開文件
?? ?FILE* pBigFile1? = fopen("..\\BigFile1.dat","rb");
?? ?if (pBigFile1 == NULL)
?? ?{
?? ??? ?printf("打開文件BigFile1.dat失敗\n");
?? ??? ?return 1;
?? ?}
?? ?FILE* pBigFile2? = fopen("..\\BigFile2.dat","rb");
?? ?if (pBigFile1 == NULL)
?? ?{
?? ??? ?printf("打開文件BigFile2.dat失敗\n");
?? ??? ?return 1;
?? ?}
?? ?//讀文件
?? ?int ret ;
?? ?ret = 0;//清零
?? ?int time1 = clock();
?? ?while (ret != -1)
?? ?{
?? ??? ?Buffer = new char[WORDSINLINE];
?? ??? ?ZeroMemory(Buffer,WORDSINLINE);
?? ??? ?ret = fscanf(pBigFile1,"%s",Buffer);
?? ??? ?MySet1.insert(Buffer);
?? ?}
?? ?ret = 0;//清零
?? ?while (ret != -1)
?? ?{
?? ??? ?Buffer = new char[WORDSINLINE];
?? ??? ?ZeroMemory(Buffer,WORDSINLINE);
?? ??? ?ret = fscanf(pBigFile2,"%s",Buffer);
?? ??? ?MySet2.insert(Buffer);
?? ?}
?? ?int time2 = clock();
?? ?printf("%s讀兩個文件花費時間為: %d(毫秒)\n",MOSHI,time2-time1);
?? ?/*
?? ?經驗與總結:讀文件的操作在Debug和Release下相差約2倍。
?? ?寫文件的操作的速度大約和讀文件的速度的差不多。
?? ?向set中insert(包括排序)是讀文件的2倍。
?? ?*/
?? ?//預覽set里面的內容
// ?? ?printf("第一個set:\n");
// ?? ?for (iter = MySet1.begin(); iter != MySet1.end(); iter++)
// ?? ?{
// ?? ??? ?printf("%s\n",*iter);
// ?? ?}
// ?? ?printf("第二個set:\n");
// ?? ?for (iter = MySet2.begin(); iter != MySet2.end(); iter++)
// ?? ?{
// ?? ??? ?printf("%s\n",*iter);
// ?? ?}
?? ?//求交集
?? ?string * begin;
?? ?string * end;
?? ?string SetIntersetion[100];//儲存交集字符串
?? ?
?? ?int time3 = clock();
?? ?end = set_intersection(MySet1.begin(),MySet1.end(),MySet2.begin(),MySet2.end(),SetIntersetion,compare());
?? ?int time4 = clock();
?? ?printf("求交過程花費時間為: %d(毫秒)\n",time4-time3);
?? ?begin = SetIntersetion;
?? ?
?? ?if (begin != end)
?? ?{
?? ??? ?printf("交集是:\n");
?? ??? ?while (begin<end)
?? ??? ?{
?? ??? ??? ?printf("%s\n",(*begin).data());
?? ??? ??? ?begin++;
?? ??? ?}
?? ?}
?? ?else
?? ?{
?? ??? ?printf("沒有發現交集\n");
?? ?}
?? ?fclose( pBigFile1 );
?? ?fclose( pBigFile2 );
?? ?return 1;
}
#include <iostream>
#pragma warning(disable : 4786)//這句話的位置必須是這里
#include<set>
#include <string>
#include <algorithm>
#include <time.h>
#include <windows.h>
using namespace std;
#define WORDSINLINE 150 //每行字節數
#ifdef _DEBUG
#define MOSHI "Debug模式下"
#else
#define MOSHI "Release模式下"
#endif
//定義一個仿函數用以實現比較方法
struct compare
{
?? ?bool operator()(const char* s1,const char* s2) const
?? ?{
?? ??? ?//bool b = strcmp(s1, s2) > 0;
?? ??? ?return (strcmp(s1, s2) < 0);//如果把b直接寫成false,則set中無論insert多少次只有一條數據。
?? ??? ??? ??? ? //如果把b直接寫成true,則系統不會進行排序,采用頭插方式。
?? ??? ??? ??? ? //總結:必須是兩者的表達式
?? ?}
};
int main(int argc, char* argv[])
{
?? ?//char Buffer[WORDSINLINE];
?? ?char *Buffer;
?? ?set<char*,compare>::iterator iter;
?? ?set<char*,compare> MySet1;
?? ?set<char*,compare> MySet2;
?? ?//打開文件
?? ?FILE* pBigFile1? = fopen("..\\BigFile1.dat","rb");
?? ?if (pBigFile1 == NULL)
?? ?{
?? ??? ?printf("打開文件BigFile1.dat失敗\n");
?? ??? ?return 1;
?? ?}
?? ?FILE* pBigFile2? = fopen("..\\BigFile2.dat","rb");
?? ?if (pBigFile1 == NULL)
?? ?{
?? ??? ?printf("打開文件BigFile2.dat失敗\n");
?? ??? ?return 1;
?? ?}
?? ?//讀文件
?? ?int ret ;
?? ?ret = 0;//清零
?? ?int time1 = clock();
?? ?while (ret != -1)
?? ?{
?? ??? ?Buffer = new char[WORDSINLINE];
?? ??? ?ZeroMemory(Buffer,WORDSINLINE);
?? ??? ?ret = fscanf(pBigFile1,"%s",Buffer);
?? ??? ?MySet1.insert(Buffer);
?? ?}
?? ?ret = 0;//清零
?? ?while (ret != -1)
?? ?{
?? ??? ?Buffer = new char[WORDSINLINE];
?? ??? ?ZeroMemory(Buffer,WORDSINLINE);
?? ??? ?ret = fscanf(pBigFile2,"%s",Buffer);
?? ??? ?MySet2.insert(Buffer);
?? ?}
?? ?int time2 = clock();
?? ?printf("%s讀兩個文件花費時間為: %d(毫秒)\n",MOSHI,time2-time1);
?? ?/*
?? ?經驗與總結:讀文件的操作在Debug和Release下相差約2倍。
?? ?寫文件的操作的速度大約和讀文件的速度的差不多。
?? ?向set中insert(包括排序)是讀文件的2倍。
?? ?*/
?? ?//預覽set里面的內容
// ?? ?printf("第一個set:\n");
// ?? ?for (iter = MySet1.begin(); iter != MySet1.end(); iter++)
// ?? ?{
// ?? ??? ?printf("%s\n",*iter);
// ?? ?}
// ?? ?printf("第二個set:\n");
// ?? ?for (iter = MySet2.begin(); iter != MySet2.end(); iter++)
// ?? ?{
// ?? ??? ?printf("%s\n",*iter);
// ?? ?}
?? ?//求交集
?? ?string * begin;
?? ?string * end;
?? ?string SetIntersetion[100];//儲存交集字符串
?? ?
?? ?int time3 = clock();
?? ?end = set_intersection(MySet1.begin(),MySet1.end(),MySet2.begin(),MySet2.end(),SetIntersetion,compare());
?? ?int time4 = clock();
?? ?printf("求交過程花費時間為: %d(毫秒)\n",time4-time3);
?? ?begin = SetIntersetion;
?? ?
?? ?if (begin != end)
?? ?{
?? ??? ?printf("交集是:\n");
?? ??? ?while (begin<end)
?? ??? ?{
?? ??? ??? ?printf("%s\n",(*begin).data());
?? ??? ??? ?begin++;
?? ??? ?}
?? ?}
?? ?else
?? ?{
?? ??? ?printf("沒有發現交集\n");
?? ?}
?? ?fclose( pBigFile1 );
?? ?fclose( pBigFile2 );
?? ?return 1;
}
總結
以上是生活随笔為你收集整理的两个大文件找出相同的一条记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: __try,__except,__fin
- 下一篇: WinPcap编程