Map自动建立Key - Value的对应。Key和Value可以是任意类型。根据Key值快速查找记录,查找的复杂度基本是Log(N)。 如果有1000个记录,最多查找10次;1,000,000个记录,最多查找20次。
Map提供的操作:
1.快速插入Key - Value记录
2.快速删除记录
3.根据Key修改Value记录
4.遍历所有记录
我现在所用到的一点点皮毛:
1.#include<map>
2.定义map
typedef std::map<CString,int> SymbolIndexMap;//记录每个合约在队列中的位置,用于筛选数据
SymbolIndexMap m_mapTickIndex;
3.使用map
(1)给map赋值
SymbolIndexMap::iterator iter;//用迭代器
iter=m_mapLineIndex.find(code);//这个是比较常用的一种查找方式,返回code所在的位置
if ( iter==m_mapLineIndex.end() )//如果返回的位置在map末尾了,说明不存在code
{
m_mapLineIndex[code] = m_LstLineData.size();//给Key(code)添加value值
}
(2)使用赋值后的map
SymbolIndexMap ::iterator iter = m_mapLineIndex.find(code);//同样定位到code的位置
uint32_t iStart = iter->second;//second指的是map定义中的第二个值,就是value。将code对应的value值取出来
iter++;
if(iter == m_mapLineIndex.end())//如果迭代器已经指向末尾
{
iEnd = m_LstLineData.size();
}
else
{
iEnd = iter->second;
}