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

有一组数据,大概几千个,需要放到内存中并且有一个关键字与其对应。 
目前的方案是使用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; 
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的有限差分法实验报告用MATLAB的有限差分法计算槽内电位;对比解析法和数值法的异同点;选取一点,绘制收敛曲线;总的三维电位图+使用说明文档 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值