用CString作为Key使用CMap

遇到好几个人说CMap在用CString做key类型时有问题,说用int和DWORD就可以,用CString就不行。因此很多人推荐使用MFC中的CMapStringToPtr之类。

看下面的代码:
//.h
    CMap<CString, LPCTSTR, int, int> typeMap;
//.cpp
    typeMap.SetAt(_T("ONE"),1);
    typeMap.SetAt(_T("TWO"),2);

    int nValue = 0;
    BOOL ret = typeMap.Lookup(_T("ONE"), nValue);
    ret = typeMap.Lookup(_T("THREE"), nValue);
    ret = typeMap.Lookup(_T("TWO"), nValue);
我的代码运行的很好,我觉得关键是ARG_KEY要选LPCTSTR

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
`CString`类提供了`Find`系列函数来查找字符串中指定子串的位置,可以帮助我们进行字符串的处理。下面是一些常用的`Find`函数: 1. `int Find(LPCTSTR lpszSub)`:在当前字符串中查找子串`lpszSub`,返回子串在当前字符串中首次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.Find("world"); if (pos != -1) { cout << "子串在字符串中的位置:" << pos << endl; } else { cout << "未找到子串" << endl; } ``` 输出为:`子串在字符串中的位置:6`。 2. `int Find(TCHAR ch)`:在当前字符串中查找字符`ch`,返回字符在当前字符串中首次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.Find('w'); if (pos != -1) { cout << "字符在字符串中的位置:" << pos << endl; } else { cout << "未找到字符" << endl; } ``` 输出为:`字符在字符串中的位置:6`。 3. `int ReverseFind(LPCTSTR lpszSub)`:在当前字符串中查找子串`lpszSub`,返回子串在当前字符串中最后一次出现的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.ReverseFind('l'); if (pos != -1) { cout << "字符在字符串中的位置:" << pos << endl; } else { cout << "未找到字符" << endl; } ``` 输出为:`字符在字符串中的位置:9`。 4. `int FindOneOf(LPCTSTR lpszCharSet)`:在当前字符串中查找字符集`lpszCharSet`中的任何一个字符,返回第一个匹配字符在当前字符串中的位置,如果找不到则返回-1。 示例代码: ```cpp CString str = "hello world"; int pos = str.FindOneOf("aeiou"); if (pos != -1) { cout << "匹配字符在字符串中的位置:" << pos << endl; } else { cout << "未找到匹配字符" << endl; } ``` 输出为:`匹配字符在字符串中的位置:1`。 除了上面的这些函数,`CString`类还提供了其他的`Find`系列函数,可以根据需要选择使用

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值