有一组数据,大概几千个,需要放到内存中并且有一个关键字与其对应。

本文探讨了在处理大量数据时,如何选择合适的容器类型以提高查找效率,重点关注map、vector以及二分查找等技术的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有一组数据,大概几千个,需要放到内存中并且有一个关键字与其对应。 
目前的方案是使用map,但是我担心其执行的效率,因为需要进行较大的比较操作 
各位高手有什么其它的建议吗?多谢!!!

你的问题说得不是很清楚: 
比如每个数据单元是不是很大(拷贝构造/赋值函数的代价):如果拷贝构造的代价比较大,那么采用map比较好,因为如果采用vector的话必须先排序,而排序会比较依赖上面两个函数。 

你所说的容器是作为一个查找池还是作为一个缓存?就是你采用什么策略?比如一次性把所有数据都读入容器再查找还是先尝试在容器中查找,找不到再去从文件中读取,类似缓存。如果是一次性的,那么可以考虑vector,读完以后一次性排序然后使用二分查找,如果可以预计数目那么先对vector进行reserve效果更好;如果是当作缓存,那么采用map,因为插入比较频繁,这个map更适合。 

map在于每次插入和删除都会保持有序状态,如果插入删除比较频繁,那么map比较好;如果是一次性读入所有数据,那么可以读入到vector后进行一次排序,因为一次性排序比每插入一个都自动排序的效率要高。 

总的说来map的速度还是不错,可以考虑使用,而且编写代码也相对比较简单。楼主不妨先用map解决问题,如果速度令人满意那就行了;如果效果较差或者心有余力那么换个方法对比一下也是可以的。实践是检验的唯一标准嘛!至于hash容器,我没有使用过,所以无法给出建议。 

下面给出一些代码,仅供参考。在尝试的时候只要把main中的前面两个typedef注释掉任何一个即可,编译的时候会自动调用不同的函数。 
data.txt: 
Jim   110   119   aaa   street1 
Tim   110   119   aaa   street2 
Jay   110   119   aaa   street3 
Tom   110   119   aaa   street4 
Kit   110   119   aaa   street5 
fangrk   110   119   aaa   street6 
Li   110   119   aaa   street7 
TTT   110   119   aaa   street8 
Kite   110   119   aaa   street9 
fol   110   119   aaa   street10 

#include   <map> 
#include   <vector> 
#include   <algorithm> 
#include   <string> 
#include   <iostream> 
#include   <fstream> 
using   namespace   std; 

namespace   MyTest 
{ 
template <class   Key,class   Value> 
void   MyInsert(std::map <Key,Value> &   cont,const   Key&   key,const   Value&   value) 
{ 
cont.insert(std::pair <Key,Value> (key,value)); 
} 

template <class   Key,class   Value> 
void   MyInsert(std::vector <   pair <Key,Value>   > &   cont,const   Key&   key,const   Value&   value) 
{ 
cont.push_back(pair <Key,Value> (key,value)); 
} 

template <class   T,class   V> 
void   MySort(std::map <T,V> &){} 
template <class   T> 
void   MySort(std::vector <T> &   cont){std::sort(cont.begin(),cont.end());} 


template <class   Key,class   Value> 
typename   std::map <Key,Value> ::const_iterator 
MyGet(std::map <Key,Value> &   cont,const   Key&   key) 
{ 
return   cont.find(key); 
} 

template <class   Key,class   Value> 
class   Pair_Key 
{ 
public: 
bool   operator()(const   Key&   K,const   pair <Key,Value> &   P)   const 
{return   K <P.first;} 
bool   operator()(const   pair <Key,Value> &   P,const   Key&   K)   const 
{return   P.first <K;} 
}; 

template <class   Key,class   Value> 
typename   std::vector <   pair <Key,Value>   > ::const_iterator 
MyGet(const   std::vector <   pair <Key,Value>   > &   cont,const   Key&   key) 
{ 
typename   std::vector <   pair <Key,Value>   > ::const_iterator   end_it=cont.end(), 
                  iter=std::lower_bound(cont.begin(),end_it,key,Pair_Key <Key,Value> ()); 
if(iter==end_it   ||   iter-> first!=key)   return   end_it; 
return   iter; 
} 


}//end   namesapce 

struct   HowToContact 
{ 
string   Tel1,Tel2,Email,Address; 
istream&   read(istream&   i){i> > Tel1> > Tel2> > Email> > Address;   return   i;} 
bool   operator <(const   HowToContact&)   const{return   true;}//没有实际作用 
}; 
istream&   operator> > (istream&   i,HowToContact&   h){return   h.read(i);} 

int   main() 
{ 
//typedef   map <string,HowToContact>   TestType; 
typedef   vector <std::pair <string,HowToContact>   >   TestType; 
TestType   Container; 
string   str; 
HowToContact   H; 
ifstream   F( "data.txt "); 
while(F> > str> > H)   MyTest::MyInsert(Container,str,H); 
MyTest::MySort(Container); 
TestType::const_iterator   iter=MyTest::MyGet(Container,string( "fangrk ")); 
if(iter!=Container.end())   cout < <iter-> first < < '/t ' < <iter-> second.Address; 
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值