CMap 集合类使用
来源:互联网 发布:js split splice区别 编辑:程序博客网 时间:2024/06/02 01:19
一:
功能要求:
需要保存单个的汉字,并且不可重复。
此功能可通过CMap容器完成。
1) 定义
//常用字占两个字节CMap<WCHAR,WCHAR,int,int> m_AddCharMap;//偏僻字占四个字节 CMap<DWORD,DWORD,int,int> m_AddPossibleCharMap;
2)设置元素
打开文本,读入常用汉字
CStdioFile readFile;if (!readFile.Open(addCharFilePath,CFile::modeRead|CFile::typeBinary)){AfxMessageBox(L"无法打开--AddChar.txt--文件");}else{WORD sign;readFile.SeekToBegin();readFile.Read(&sign,2);if (sign!=0xfeff){AfxMessageBox(L"常用字.txt 不是Unicode");}else{WCHAR str;while (readFile.Read(&str,2)){//判断是否为常用汉字if(str>=0x4e00&&str<=0x9fa5)m_AddCharMap.SetAt(str,0);}}}
因为,WCHAR str 是关键字,因此使用CMap保证了它不会重复
如果是偏僻字,则加入偏僻字容器中
//BMP之外的辅助字if (pWord->Str[1]!=0x0000){DWORD d;memcpy((void *)&d,pWord->Str,4);// 加入偏僻字容器中m_AddPossibleCharMap.SetAt(d,0);}
3)遍历容器
CMap<WCHAR,WCHAR,int,int>::CPair* pCurValue=m_AddCharMap.PGetFirstAssoc();WCHAR ch;while (pCurValue!=NULL){ch=pCurValue->key;wFile.Write(&ch,2);wFile.WriteString(L"\r\n");pCurValue=m_AddCharMap.PGetNextAssoc(pCurValue);}
二:
KEY是CString 类型的字符串,且无重复
1)定义
//文件路径CMap<CString,LPCTSTR,int,int> m_filePathMap;
注意: 第二个参数为LPCTSTR
而 CMap<CString, CString, int ,int > 这样定义是错误的
2) 设置元素
//记录文件路径 add by yujian 5-10m_filePathMap.SetAt(szCurXmlFilePath,0);
3) 遍历元素
CMap<CString,LPCTSTR,int, int>::CPair *pCurValue=m_filePathMap.PGetFirstAssoc();CString str;while (pCurValue){str=pCurValue->key;pCurValue=m_filePathMap.PGetNextAssoc(pCurValue);}
三:
CMap 元素未排序 如何排序?
解决途径: 遍历元素,将其存放到list容器中, 使用list容器对其排序
list容器的头文件:
#include <list>
遍历CMap容器,并依次将元素放入list容器中, 最后对list容器进行排序
// 使用容器前,加上命名空间using namespace std;//list 容器list <CString> fileList;CMap<CString,LPCTSTR,int, int>::CPair *pCurValue=m_filePathMap.PGetFirstAssoc();CString str;while (pCurValue){str=pCurValue->key;//置入list容器中fileList.push_back(str);pCurValue=m_filePathMap.PGetNextAssoc(pCurValue);}//对list容器进行排序fileList.sort();
遍历list容器,将数据保存到文件中
//遍历list容器list<CString>::iterator it=fileList.begin();for (it=fileList.begin();it!=fileList.end();it++){str=*it;wFile.Write(str.GetBuffer(),str.GetLength()*2);str.ReleaseBuffer();wFile.WriteString(L"\r\n");}
- CMap 集合类使用
- CMap类的使用
- 使用MFC中的CMap类
- MFC中 CMap 类使用
- MFC中的CMap类使用
- CMap使用
- CMap使用
- CMap类
- CMap使用自定义类的例子
- CMap 使用注意说明
- CMap 的使用
- CMap 的使用
- CMap简单使用
- CArray和CMap使用
- MFC CMap使用总结
- CMap类的用法
- CMap类的用法
- 映射表类(CMap)
- makefile 实例一
- 静or动态库中的全局变量被不同的dll调用修改,其值是否独立?
- android完全退出程序 (所有版本通用)
- Android apk系列1-------APK签名
- 程序员面试题精选100题(10)-排序数组中和为给定值的两个数字[算法]
- CMap 集合类使用
- Android Mms 数据库
- VC++ GetLastError
- m2-Maven编译遇到的不支持注释问题
- 正确获取硬盘序列号源码
- 使用PreparedStatement防止SQL注入
- 查看oracle的服务名
- ch08 替换
- 用linq查询怎么实现唯一distinct的查询效果