锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 英语翻译 / C++开源英语 / STL的map翻译
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。

英文原文

如何配合类使用map

map是一个模板,使用键获取一个值。
另一个问题是,你将要使用像你自己的类,而不是数据类型,上面示例里一直使用int到现在 。要创建一个“为模板准备好”的类,你必须确保类包含一些成员函数和运算符 。基本上有:

  • 默认构造函数(空的,通常)
  • 拷贝构造函数 克隆
  • 重载“ = “

对于一个特定的模板,会有额外的要求把其它操作符重载下,例如,如果你计划用map,你将必须重载关系运算符。但是,本文对此并不描述。

// Program: Map Own Class
// Purpose: To demonstrate a map of classes
#include <string> 
#include <iostream>
#include <vector>
#include <map> 
using namespace std;
class CStudent
{
public :
int nStudentID;
int nAge;
public :
// Default Constructor - Empty
CStudent()  {  }
// Full constructor
CStudent(int nSID, int nA)  {  nStudentID=nSID; nAge=nA;  }
// Copy constructor
CStudent(const CStudent& ob) 
{  nStudentID=ob.nStudentID; nAge=ob.nAge;  }
// Overload =
void operator = (const CStudent& ob) 
{  nStudentID=ob.nStudentID; nAge=ob.nAge;  }
};
 
int main(int argc, char* argv[])
{
map <string, CStudent> mapStudent;
mapStudent["Joe Lennon"] = CStudent(103547, 22);
mapStudent["Phil McCartney"] = CStudent(100723, 22);
mapStudent["Raoul Starr"] = CStudent(107350, 24);
mapStudent["Gordon Hamilton"] = CStudent(102330, 22);
 
// Access via the name
cout << "The Student number for Joe Lennon is " << 
(mapStudent["Joe Lennon"].nStudentID) << endl;
return 0;
}

一些小技巧

为了避免调试模式下一些烦人的错误,使用以下编译器用法:

#pragma warning(disable: 4786)

另一种疑难杂症是:你必须确保,在尖括号和名称之间要插入空格。这是因为> >会看起来象位的移位运算符,所以 :

vector <list<int>> veclis;

将给出一个错误。相反,把它写:  

vector <list <int> > veclis;

以避免编译错误。

友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内