【问题标题】:Operator overloading in map/pair映射/对中的运算符重载
【发布时间】:2014-11-07 15:14:09
【问题描述】:

我试图了解 STL 类模板中使用的运算符重载,例如:映射或对。

让我给你介绍一下我的代码:

#include <iostream> 
#include <iomanip>  // left, setw
#include <string> 
#include <map> 
#include <utility> // pair 
#include <algorithm> // count if
using namespace std; 

typedef pair <string, int> Emp; 
typedef map <string, Emp> MAP; 

class Zakr{
   int min, max; 
public: 
   Zakr(int min, int max): min(min), max(max){}

bool operator()(const pair<const string, Emp> &p) const{
    int wage = p.second.second; 
    return (min < wage) && (wage < max); 
}
}; 

void print (const MAP& m) {
   MAP::const_iterator it, fin = m.end(); 

for(it = m.begin(); it != fin; it++)
    cout << "Key:  " << left << setw(7) << it -> first
    << "Name:  " << setw(10) << it->second.first 
    << "Wage:  " << it->second.second << endl; 
}

int main(void){
   MAP emp; 

   MAP object; 

   emp["John"] = Emp("John K.", 1900); 
   emp["Tania"] = Emp("Tania L.", 1900); 
   emp["Jeremy"] = Emp("Jeremy C", 2100); 
   emp["Susie"] = Emp("Susie W.", 3100); 
   emp["Toto"] = Emp("Toto T.", 9900); 
   emp["Adrian"] = Emp("Adrian N.", 1600); 
   emp["Germy"] = Emp("Germy P.", 2600); 

 print(emp); 

 int mn = 0, mx = 2000; 
 int how_much = count_if(emp.begin(), emp.end(), Zakr(mn, mx)); 

 cout << how_much << " earn from" 
 << mn << " to " << mx << endl;
 }

我正在努力理解一些位,尤其是其中一个,即:

class Zakr{
   int min, max; 
public: 
   Zakr(int min, int max): min(min), max(max){}

bool operator()(const pair<const string, Emp> &p) const{
    int wage = p.second.second; 
    return (min < wage) && (wage < max); 
}
}; 

所以我构建了一个名为 Zakr 的类,这样我就可以使用它来确定 count_if 语句中的函子。

我说的对吗?

我初始化私有字段 min 和 max 以在构造函数中使用它们,然后重载的运算符可以根据它们自己的值返回布尔值。 最困难的部分是理解布尔运算符重载。

 bool operator()(const pair<const string, Emp> &p) const{
    int wage = p.second.second;

为什么我需要 1* 制作一对无用的字符串值和 EMP?

由于我感兴趣的所有内容都存储在 EMP 中,即:将用于重载的 int 值存储在 Emp 中。

为什么我不能像这样访问存储在 Emp 中的 int:

bool operator(Emp &p)
{
  int wage = p.second; 
  return (min < wage) && (wage < max); 
} 

为什么我需要像这样制作另一对:(const pair &p),如果我感兴趣的只是存储在名为 Emp 的对中的值。

为什么我需要用上面命名对的无用第一个元素制作另一对:字符串。 我不会用它,那为什么要编译代码呢?

我确实尽力尽可能清楚地解释我的疑问。 希望有人能理解这篇相当长的帖子。

干杯!

【问题讨论】:

    标签: c++ operator-overloading


    【解决方案1】:

    这是因为std::map 上的迭代器会为每个元素返回一个std::pair。该对中的第一项是映射键,第二项是映射值。请参阅documentation for std::map 中的value_type

    This question 有一些关于如何仅获取地图值的迭代器的答案。

    【讨论】:

    • 不知道这个,我真的很高兴它如此简单,而且你为我提供了这个易于理解的解释。谢谢!
    【解决方案2】:

    1. Zakr 类:

    确实,它通常用于count_if()。如果您查看链接,您会看到 count_if(first, last, pred) 相当于:

      int ret = 0;
      while (first!=last) {
        if (pred(*first)) ++ret;  // works as soon as object pred has operator() defined. 
        ++first;
      }
      return ret;
    

    2。为什么 operator() 中需要一对:

    map 与对一起使用,每个由唯一的键和对应的值组成。

    这部分是隐藏的。例如,当您将映射用作关联数组,表达式为emp["John"] 时,映射将找到具有唯一键“John”的对并返回对相应值的引用。

    但是,一旦您遍历地图,您的迭代器就会处理这些对。为什么 ?因为如果它只是遍历值,您将获得值,但您永远不会知道它对应于哪个唯一键。

    结果:count_if() 遍历映射,因此使用寻址一对的迭代器调用谓词。

    3.为什么要做一个无用的对:

    首先,计数函数不会创建虚拟对。它使用对现有对的引用(从性能的角度来看,它不比传递指针成本高!)

    而且,地图是用来解决一般问题的。有朝一日,您可能有兴趣不仅计算工资,还计算关联的键(例如:姓名以“A”开头的员工的所有工资范围内的所有工资)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-13
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2015-09-27
      相关资源
      最近更新 更多