STL 中容器 map 的使用
来源:互联网 发布:ps字体下载for mac 编辑:程序博客网 时间:2024/04/30 11:21
以下代码实现stl中容器 map 的简单操作。包括插入,更新,遍历,查找。
#pragma once#include "string"#include "iostream"#include "map"using namespace std;class CStudent{string name;int english;public:CStudent(void);~CStudent(void);CStudent(string name, int english);public:void inline printit(){cout<<"name=";cout<<name;cout<<" english=";cout<<english;cout<<endl;}};#include "stdafx.h"#include "Student.h"CStudent::CStudent(void){}CStudent::CStudent(string name, int english){this->name = name;this->english = english;}CStudent::~CStudent(void){}
#pragma once#include "Student.h"#include "map"using namespace std;class CStudentMap{public:CStudentMap(void);~CStudentMap(void);private:map<int, CStudent> m_studentmap;public:bool insert(int number, CStudent stu);bool remove(int number);bool update(int number, CStudent stu);bool find(int number, CStudent& stu);void output(); int count();};#include "stdafx.h"#include "StudentMap.h"#include "algorithm"CStudentMap::CStudentMap(void){}CStudentMap::~CStudentMap(void){}int CStudentMap::count(){return m_studentmap.size();}bool CStudentMap::insert(int number, CStudent stu){pair<map<int, CStudent>::iterator, bool> Insert_Pair;Insert_Pair = m_studentmap.insert(pair<int, CStudent>(number, stu));if(Insert_Pair.second){return true;}else{return false;}/*Insert_Pair = m_studentmap.insert(map<int, CStudent>::value_type(number, stu));if(Insert_Pair.second == true){return true;}else{return false;}*/}bool CStudentMap::remove(int number){map<int, CStudent>::iterator LI;LI = m_studentmap.find(number);if (LI != m_studentmap.end()){m_studentmap.erase(LI);return true;}else{return false;}}bool CStudentMap::update(int number, CStudent stu){m_studentmap[number] = stu;return true;}void CStudentMap::output(){map<int, CStudent>::iterator LI;for(LI = m_studentmap.begin(); LI != m_studentmap.end(); LI++){LI->second.printit();}}bool CStudentMap::find(int number, CStudent &stu){map<int, CStudent>::iterator LI;LI = m_studentmap.find(number);if (LI != m_studentmap.end()){stu = (LI)->second;return true;}else{return false;}}
以下为主函数测试:
// stlmap.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "StudentMap.h"int _tmain(int argc, _TCHAR* argv[]){CStudentMap smap;CStudent stu[10];for(int i=0; i<10; i++){char buff[100];sprintf_s(buff, 100 ,"zhang%02d",i);string name(buff);CStudent student(name, i*10);stu[i] = student;smap.insert(i, stu[i]);}smap.output();bool ret1 = smap.insert(0, stu[0]);bool ret = smap.find(15, stu[8]);cout<<"************************"<<endl;if (ret){stu[8].printit();}else {cout<<"not find"<<endl;}system("pause");return 0;}
- STL 中容器 map 的使用
- C++ STL 中 map 容器的说明和使用技巧
- C++ STL 中 map 容器的说明和使用技巧 .
- C++ STL 中 map 容器的说明和使用技巧
- C++ STL 中 map 容器的说明和使用技巧
- C++ STL 中 map 容器的说明和使用技巧
- stl map容器中指针的释放
- STL中map容器的元素插入
- STL中map容器的操作
- STL 中map 容器的基本用法
- STL中关联容器map的用法
- STL中map容器的一点点用法
- STL中map的使用
- STL中 MAP的使用
- STL中map的使用
- STL中map的使用
- STL中map的使用
- STL中map的使用
- Android webview 中添加背景
- 创建Windows 7系统修复光盘方法
- object c 数组遍历与排序
- Android 图片合成:添加蒙板效果 不规则相框 透明度渐变效果的实现
- 利用光标获取QTextEdit的行号
- STL 中容器 map 的使用
- Asp.Net 三层式Web应用程序开发
- Linux设置软链接(以Ubuntu12.04为例)
- C#中ref和out的使用小结
- 升级ADT v22后出现第三方包中的类java.lang.ClassNotFoundException解决
- 你得学会并且学得会的Socket编程基础知识
- 全方位解析 Web Services 开发步骤
- Linux下 利用find命令删除所有.svn目录
- 软刷BIOS激活Windows 7会损坏主板吗?