CMap使用方法总结
程序员文章站
2024-01-07 20:36:16
#include #ifdef _DEBUG #include #include using std::endl; #endif void CMFCApplication1Dlg::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 #ifdef _DEBUG std... ......
#include <array>
#ifdef _debug
#include <iostream>
#include <fstream>
using std::endl;
#endif
void cmfcapplication1dlg::onbnclickedok()
{
// todo: 在此添加控件通知处理程序代码
#ifdef _debug
std::ofstream ofs("log.txt");
#endif
// 使用setat()初始化
typedef cmap<cstring, cstring, cstring, cstring> map_employee;
map_employee map_employee;
std::array<cstring, 3> employee_id{"100","108","120"};
std::array<cstring, 3>employee_name{_t("shihuan"), _t("lipeng"), _t("tianjunhong")};
for (auto i=0; i!=employee_id.size(); ++i)
{
map_employee.setat(employee_id[i], employee_name[i]);
}
// 使用pos遍历
position pos = map_employee.getstartposition();
while (pos)
{
cstring key = 0;
cstring value;
map_employee.getnextassoc(pos, key, value);
#ifdef _debug
ofs << key.getstring() << " " << value.getstring() << endl;
#endif
}
// 使用cpair* 遍历
auto p = map_employee.pgetfirstassoc();
while (p != null)
{
#ifdef _debug
ofs << p->key.getstring() << " " << p->value.getstring() << endl;
#endif
p = map_employee.pgetnextassoc(p);
}
#ifdef _debug
ofs.close();
#endif
cstring value;
bool ret;
ret = map_employee.lookup("100", value); // 查找:未找到返回0
assert(0 == ret);
ret = map_employee.removekey("100"); // 删除:不存在返回0
assert(0 != ret);
map_employee.removeall(); // 清空
cdialogex::onok();
}