请问如何用c或c++对几十万行文本进行排序、删除重复行?

amo73 2007-07-19 04:34:58
方法有不少:
1-导入数据库,排序、删除重复行,输出;
2-用工具软件,如UE;
3-在linux下先sort后uniq;

但是现在需要集成到自己的程序里,希望大家能给出较详细的代码例子,谢谢!
...全文
1735 33 打赏 收藏 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
浪里个儿浪 2012-03-20
  • 打赏
  • 举报
回复
楼主,求您的主函数呀!
sylvanaszm 2008-12-12
  • 打赏
  • 举报
回复
mark
lnuzwy 2008-11-06
  • 打赏
  • 举报
回复
上空飘过~
lann64 2008-10-26
  • 打赏
  • 举报
回复
怎么看都是脚本语言最简单。
yjuufgef 2008-10-26
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 lbaby 的回复:]
推荐直接用sort
或者使用脚本,省时,省下的时间可以用来陪MM。

何乐而不为呢?
[/Quote]
laf~
iGarlic 2008-10-22
  • 打赏
  • 举报
回复
用stl的map就行了吧
勇敢的天牛 2008-07-29
  • 打赏
  • 举报
回复
以上方法,我保证比数据库快很多倍
勇敢的天牛 2008-07-29
  • 打赏
  • 举报
回复
几十万行数据排序还是比较简单的,按100万算吧,一条256字节,极限消耗244M的内存,基本消耗和你文件大小一样
所以直接把数据读进内存就好了。
数据结构上用哈希表呗,好处是自动排重,同时节约内存。
取前四个字节,有456976中情况,对于100万数据,速度应该很理想,内存消耗不足2M
重复的时候使用strcmp排序,大概只比较两个串大小,应该很快,除非你的数据有规律
如果你的数据有规律比如都是http://这种开头,就要提前处理下,要不就死定了
qjyh 2008-06-29
  • 打赏
  • 举报
回复
定义一个有序set,自己指定比较器,读取文件每一行,插入该set,输出该set。
liveforlov 2008-06-25
  • 打赏
  • 举报
回复
用awk+脚本,速度比你的代码快多了
UltraBejing 2008-04-30
  • 打赏
  • 举报
回复
不明LZ在说什么
Super.Jiju 2008-03-02
  • 打赏
  • 举报
回复
用hash吧
Google他们就是用这样的方式来删除重复网址的吧
如果仅仅用hash似乎不能排序哎

可以做个参考:
http://blog.csdn.net/jiju8484/archive/2008/03/01/2137499.aspx
gunsand 2008-01-24
  • 打赏
  • 举报
回复
用脚本。比较EASY。
jsphuang 2007-11-09
  • 打赏
  • 举报
回复
用C/C++还是导入数据库比较容易处理
neversaydie518 2007-10-29
  • 打赏
  • 举报
回复
晕的
lbaby 2007-10-10
  • 打赏
  • 举报
回复
推荐直接用sort
或者使用脚本,省时,省下的时间可以用来陪MM。

何乐而不为呢?
waterfield 2007-09-17
  • 打赏
  • 举报
回复
用范型sort 和unique 应该比较简单吧
loops 2007-09-13
  • 打赏
  • 举报
回复
上面错了,请增加一个
bool pr2( const StringIndex::value_type &l, const StringIndex::value_type &r )
{
if( l.second!=r.second ){ return false; }
if( strcmp(l.first, r.first )==0 ){return true;}
return false;
}
并且将index.erase(unique(index.begin(),index.end(),pr1),index.end());改成
index.erase(unique(index.begin(),index.end(),pr2),index.end());
loops 2007-09-13
  • 打赏
  • 举报
回复
#include <vector>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define LARGENUMBER 6293815
class CHashString
{
public:
CHashString(){ InitHash(); }
void InitHash()
{
index = 1;
sum = 0;
multiple = LARGENUMBER;
}
unsigned long ConsumeChar( char c )
{
sum+=multiple*index++*c;
multiple*=LARGENUMBER;
return sum;
}
unsigned long GetCurSum(){ return sum; }
public:
int index;
unsigned long sum;
unsigned long multiple;
};


typedef vector<pair<char*, unsigned long> > StringIndex;//first 是指针,second是hash值
class CreateStringIndex
{
public:
CreateStringIndex():buffer(NULL){}
~CreateStringIndex(){ delete []buffer; }
StringIndex & GetStringIndex(){ return index; }
bool Create(const char *pFileName )
{
size_t pInd=0;
CHashString hashString;
StringIndex::value_type temp;
ifstream infile( pFileName, ios::binary);
if( infile.fail() ){
return false;
}
infile.seekg(0,ios::end);
length=infile.tellg();
infile.seekg(0,ios::beg);
delete []buffer;
buffer = new char[length+1];
istreambuf_iterator<char> it(infile);
istreambuf_iterator<char> ed;
while( pInd < length )
{
if( *it == '\r' || *it == '\n' )
{
buffer[pInd++]=0;
++it;
if( temp.first )
{
temp.second = hashString.GetCurSum();
index.push_back( temp );
}
hashString.InitHash();
temp.first = NULL;
}else
{
hashString.ConsumeChar( *it );
if( temp.first== NULL )
{
temp.first = &buffer[pInd];
}
buffer[pInd++] = *it++;
}
}
if( temp.first ){//如果文件末尾不是以\n结束的
buffer[pInd]=0;
temp.second = hashString.GetCurSum();
index.push_back( temp );
}
return true;
}
private:
char *buffer;
size_t length;
StringIndex index;
};
bool pr1( const StringIndex::value_type &l, const StringIndex::value_type &r )
{
if( l.second == r.second )
{
return (strcmp(l.first, r.first )<0);
}else
{
return l.second<r.second;
}
}
int main()
{
CreateStringIndex createIndex;
if( !createIndex.Create("test.txt") ){return 0;}
StringIndex &index = createIndex.GetStringIndex();
sort(index.begin(),index.end(),pr1);
index.erase(unique(index.begin(),index.end(),pr1),index.end());
for( StringIndex::iterator it = index.begin(); it!=index.end(); it++ ){
cout<<it->first<<endl;
}
return 0;
}
wishfly 2007-09-13
  • 打赏
  • 举报
回复
mark
加载更多回复(13)

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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