c++ - 重载箭头 ( -> ) 运算符 C++ 的问题

标签 c++ templates operators iteration overloading

我有一个自定义容器来存储数据和一个迭代器来访问它。问题是迭代器中的 -> 运算符不允许我修改容器中的数据。

value_type 是一个 std::pair<>

数据持有者:

template <class tree>
struct avl_node {
private:
    typedef typename tree::key_type         Key;
    typedef typename tree::mapped_type      Type;
    typedef typename tree::value_type       value_type;
public:
    typedef avl_node*                       node_ptr;
    typedef const avl_node*                 const_node_ptr;
    node_ptr _parentNode, _leftNode, _rightNode;
    int _balance;
    Key first;
    Type second;
}

迭代器:

template <class tree>
class avl_iterator {
public:
    typedef typename tree::node             node;
    typedef typename tree::node_ptr         node_ptr;
    typedef typename tree::const_node_ptr   const_node_ptr;
    typedef typename tree::utilities        utilities;
    typedef typename tree::value_type       value_type;
private:
    node_ptr _node;
public:
    avl_iterator() : _node()  { }
    avl_iterator( const node_ptr node ) : _node ( node ) { }
    ~avl_iterator() { _node = NULL; }

    avl_iterator& operator=(const avl_iterator& rhs) {
        _node = rhs._node;
        return (*this);
    }
    bool operator == ( const avl_iterator& rhs ) { return ( _node == rhs._node); }
    bool operator != ( const avl_iterator& rhs ) { return ( _node != rhs._node); }

    avl_iterator& operator++()
    {
        _node = utilities::next_node( _node );
        return (*this);
    }
    avl_iterator& operator ++( int ) {
        avl_iterator temp(*this);
        ++(*this);
        return(temp);
    }

    avl_iterator& operator -- () {
        _node = utilities::prev_node( _node );
        return (*this);
    }

    avl_iterator& operator -- ( int ) {
        avl_iterator temp(*this);
        --(*this);
        return(temp);
    }

    value_type& operator * () {
        return value_type( node->first, node->second );
    }

    node_ptr operator -> () const {
        return _node;
    }
};

主要内容

    try{
    using namespace avl;
    avltree<int,int> tree;
    tree.insert(intPair(1,1));
    tree.insert(intPair(2,1));
    tree.insert(intPair(3,1));
    tree.insert(intPair(4,1));
    tree.insert(intPair(5,1));
    tree.insert(intPair(6,1));
    tree.insert(intPair(7,1));
    tree.insert(intPair(8,1));
    tree.insert(intPair(9,1));
    tree.insert(intPair(10,1));
    tree.insert(intPair(11,1));
    tree.insert(intPair(12,1));

    avltree<int,int>::iterator it;
    //std::map<int,int>::iterator it;
    for( it = tree.begin(); it != tree.end(); ++it )
    {
        std::cout<< it->first << " : ";
    }
    std::cout << std::endl;
    for( it = tree.end(); it != tree.begin(); --it )
    {
        std::cout<< it->first << " : ";
    }
    std::cout << std::endl;

            it->first = 5;//shouldnt compile with this line of code, but does
            it->second = 10;
}
catch(...)
{
    std::cout<<"Catch Block\n";
}

最佳答案

如果有就更好了

value_type value;

avl_node 中而不是:

Key first;
Type second;

然后,您可以在avl_iterator中定义:

value_type& operator * () const {
    return _node->value;
}

value_type* operator -> () const {
    return &_node->value;
}

关于c++ - 重载箭头 ( -> ) 运算符 C++ 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15046243/

相关文章:

c++ - tm中使用的值1900是否定义为符号?

c++ - 为什么在我使用模板时 VS 不自动完成?

c++ - 模板 : one works, 中的方法没有实例化

javascript - 算术运算符如何计算 JavaScript 中的无穷大值?

c++ - 这个函数有什么问题

c++ - 具有未使用模板参数的函数模板

c++ - 使用私有(private)方法设置类变量

C++11 构造函数重载解析和初始化列表 : clang++ and g++ disagree

python - "is"运算符对整数的行为异常

operators - 什么是 XAND 和 XOR