【问题标题】:Postincrementation operator in linked list链表中的后增量运算符
【发布时间】:2014-03-03 00:20:58
【问题描述】:

我必须编写一个程序来处理这个主代码:(不允许更改它)

list<int> iv;

  iv["john"] = 23;

  int ia = iv["john"]++;
  int ib = iv["john"];

  cout << ia << " " << ib << endl; // prints 23 24
  try{
  cout << iv["jack"] << endl;   // should throw an exception
  }catch(list<int>::Uninitialized&)
  {
    cout << "Uninitialized map element!" << endl;
  };

这是我的代码:

#ifndef EXAM_H
#define EXAM_H
#include <iostream>
#include <string>

using namespace std;

template <class TYPE>
class list
{
private:
struct node
{
  TYPE value;
  string index;
  bool isInit;
  node *next;
};
node *head;
node *current;
public:
  class Cref
  {
    friend class list;
    list& s;
    string position;
    Cref (list& ss, string pos): s(ss), position(pos) {};
  public:
    operator TYPE() const
    {
      return s.read(position);
    }
    Cref& operator = (TYPE val)
    {
      s.write(position,val);
      return *this;
    };
    Cref& operator = (const Cref& ref)
    {
      return operator= ((TYPE)ref);
    };
  };
  class Uninitialized{};
  list ()
  {
    cout << "constructor\n";
    head = NULL;
    current = NULL;
  }

  ~list ()
  {
    while (head)
      {
        node *t = head->next;
        delete head;
        head = t;
      };
  }

  TYPE read (string ind) const
    {
      cout << "read\n";
      node *t = head;
      while(t)
      {
        if(t->index == ind && t->isInit == true)    return t->value;
        else t = t->next;
      }
      throw Uninitialized();
    }  

void write (string ind, TYPE value_)
{
 cout << "write\n";
 node *t = new node;
 t->next = head;
 head = t;
 head->value = value_;
 head->index = ind;
 head->isInit = true;
}  

TYPE operator[] (string ind) const
{
 cout << "read\n";
      node *t = head;
      while(t)
      {
        if(t->index == ind && t->isInit == true)    return t->value;
        else t = t->next;
      }
      throw Uninitialized();
}

Cref operator[] (string ind)
{
  return Cref(*this, ind);
}

};
#endif

一切都很好,但只有当我在主程序中注释掉后增量操作时

int ia = iv["john"]++;

如您所见,我有一个结构节点,我在其中放置了所有变量,并且我想在键为“john”的节点中将值递增一。有什么方法可以为此代码实现 operator++ 吗? 我不允许使用 std::map。

【问题讨论】:

  • 与问题没有直接关系,但using namespace std; 在全局命名空间中是一个非常糟糕的主意 - 特别是如果您声明自己的名为list 的类。
  • 如果不对operator TYPE() 操作员的工作方式进行返工,我认为您不会很快解决此问题。正如所写的那样,没有机会增加基础数据,因为没有对它的引用。一切都是 by-val 副本。
  • 好的,我明白了。谢谢你。我将使用向量编写另一个版本

标签: c++ linked-list operator-overloading


【解决方案1】:

是的,我已经尝试过这个解决方案,但是编译器不区分读写并且仍然使用非常量版本。所以我不得不构建有助于区分的代理类 Cref。 我也已经找到了 operator++ 问题的解决方案。 此操作必须来自 Cref 级别。我创建了

Cref& operator++ (int val)
    {
      s.increment(position,val);
      return *this;
    };

主类体中的自增函数如下:

void increment (string ind, int value_)
{
 cout << "increment\n";
 node *t = head;
 while(t)
 {
    if(t->index == ind && t->isInit == true)    t->value = t->value + 1;
    t = t->next;
 }
}  

这完全解决了我的问题。

【讨论】:

    【解决方案2】:

    解决问题的常用方法是将数组下标运算符定义为

    const TYPE& operator[](string ind) const;
    TYPE& operator[](string ind);
    

    这样一来,您就不必为operator++操心了:由于iv["John"]返回一个对int的引用,iv["John"]++将调用int构建的后增量运算符-在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 2010-10-14
      • 2016-02-02
      • 1970-01-01
      • 2011-12-21
      • 2020-07-14
      相关资源
      最近更新 更多