首页 > 其他 > 详细

CMap使用方法总结

时间:2019-02-20 11:54:12      阅读:206      评论:0      收藏:0      [点我收藏+]
#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();
}

  

CMap使用方法总结

原文:https://www.cnblogs.com/manongdashu/p/10405441.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!