STLport 双重排序和容器选择的问题
要处理一些大的文本文件,7.8十兆,有100万行数据. 格式如下:
time a b
07:01:31:22:28:17 163 1174
07:01:31:23:23:18 3 38
07:01:31:23:24:06 1 123
07:01:31:23:37:56 129 1742
07:01:31:23:52:51 3 12
07:02:01:00:04:47 32 1225
07:02:01:00:06:04 1289 93
07:02:01:00:07:26 71 5871
07:02:01:00:07:59 6994 37
上面是file中一段数据。 先根据time选择要处理文件的长度,比如说07:01:31:22:28:17到07:02:31:22:28:17这一个月的。
然后对a,b两列处理。
1 compare a with b, if a> b, then change a and b
2 if a == b, then delete this entry
3 sort column a in increasing order //a移动的时候它对应的b跟着一起移动。 整个entry(a,b)跟着移动
then sort column b in increasing order for every value of a
4 delete same entry eg: 12 34
12 34 //delete
12 34 //delete
just keep one entry.
The last file is like this: (no time column)
a b
1 2
1 3
1 4
2 3
2 4
2 5
2 6
3 4
3 5
3 6
我比较怵指针和链表,所以想用STL封装好的。
struct email
{
srting sent_time;
long int sender;
long int receiver;
};
vector <email> email
因为考虑到文件大小不定,就想用STLport的vector,后面又牵扯到根据a,b的双重排序,不晓得sort函数支持不支持。脑子乱七八糟又想用multimap或者hash table,根据key/value来排序和删除。
上面的结构体也许也用不到,读文件每读一行,就抽取a,b放在vector或者map里。那个时间范围的截取,我需要用强制类型转换把字符串型弄成data吗?这个怎么处理呢?我自己思路挺乱的,请各位帮忙指点一下。多谢!
BTW:我用的是VC6+SP6+STLport4.6.2
[解决办法]
struct email
{
srting sent_time;
long int sender;
long int receiver;
bool operator <(email& des);
};
bool email::operator <(email& des)
{
先比较时间,由于你是字符串所以需要自己写代码比较了,
然后在比较sender
}
最后直接
vector <email> v;
sort(v.begin(),v.end() );这样就好了,关键是那个email::operator <(email& des)你得些对
[解决办法]
LS正确。
可以通过自定义operator < 来描述你希望的顺序。它表达了排序语义,如order by time,a,b 。如果在插入时就过滤time,可以将operator < 写成 order by a,b 的语义
bool email::operator <(email& des) // order by a,b
{
if (a < des.a) return true;
else if (a == des.a && b < des.b) return true;
else return false;
}
此外,lower_bound的语义是> =,upper_bound的语义是> ,如果开始是order by time,a,b ,那么可以用这两个函数界定满足条件的记录起止位置。