努力理解C ++中的一段代码 [英] Struggling to understand a piece of code in C++

查看:75
本文介绍了努力理解C ++中的一段代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

template <class _Key, class _Tp, class _Compare=less<_Key> >
class IsMap : public map<_Key, _Tp, _Compare>
{
public:
_Tp& operator[](const _Key& __k) throw (ExptAppl)
{
for (IsMap<_Key, _Tp, _Compare>::iterator it = this->begin();
it != this->end(); it++)
if ((*it).first == __k)
return (*it).second;
string errMsg = "Element <" + __k + "> does not exist";
throw ExptAppl("", 0, 3003, "", (char*)errMsg.c_str());
}
};




引用:

我是什么理解?



IsMap是一个继承类地图的类模板。所以这里使用的通用数据类型是:

_key - >这表示密钥。

_Tp - >此表示类型。

_Compare = less< _key> - >比较功能。

现在我们需要了解Map的工作原理吗?

std :: map< key_type,data_type,[comparison>

关键比较功能,一个参数类型为key_type的排序;如果第一个参数小于第二个参数,则返回true,否则返回false。这也被定义为map :: key_compare。这意味着如果你想要检索存储在地图中的每个键值对,你可以按键的顺序检索它们。



我没有做什么?

就行:_Tp& operator [](const _Key& __k)throw(ExptAppl)

剩下的我已经猜到了。



能否请你帮我理解这个通过识别这段缺失的逻辑来完成整段代码?



谢谢

Deb

What I understood?

IsMap is a class template which inherits the class map. So here the generic Data type used here are:
_key -> This Denotes Key.
_Tp -> This Denotes Type.
_Compare=less<_key> -> A comparison function.
Now we need to understand how Map works?
std::map <key_type,data_type,[comparison>
The key comparison function, an ordering whose argument type is key_type; it returns true if its first argument is less than its second argument, and false otherwise. This is also defined as map::key_compare.This means that if you want to retrieve every key, value pair stored in the map, you can retrieve them in the order of the keys.

What I did not?
just the line : _Tp& operator[](const _Key& __k) throw (ExptAppl)
The Rest I have guessed.

Can you please help me understanding this whole piece of code by identifying this missing piece of logic?

Thanks
Deb

推荐答案

在此处找到解决方案:

http ://www.cplusplus.com/reference/map/map/operator [] / [ ^ ]
Found the solution here :
http://www.cplusplus.com/reference/map/map/operator[]/[^]


Quote:

在这里找到解决方案:

http://www.cplusplus.com/reference/map/map/operator

Found the solution here :
http://www.cplusplus.com/reference/map/map/operator


这篇关于努力理解C ++中的一段代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆