Maverick94
Maverick94

Reputation: 257

Iterator in C++

I'm trying to create my own translator. It's university work. I need an iterator in my class Translator.

class Translator
{
private:
    map <string,Word> translator;

public:
    class iterator
    {
       friend class Translator;
        private:
            map<string,Word>::iterator itm;

        public:
            iterator operator++();
            pair <string,Word> &operator*();
            bool operator==(const iterator &it)const;
    };
};

I'm trying to overload operator*();

This is the code.

pair <string, Word>& Translator::iterator::operator*()
{
  return (*itm);
}

Error:

invalid initialization of reference of type ‘std::pair<std::basic_string<char>, Word>&’ from expression of type ‘std::pair<const std::basic_string<char>, Word>

Upvotes: 5

Views: 494

Answers (2)

geoalgo
geoalgo

Reputation: 688

It is more a complement than an actual answer but if you want a nice iterator (STL complient) you will also need to add several typedefs, to say for instance the type of your iterator (in your case you have an input iterator). Then you can use your iterator with any stl algorithm which can be very nice. However this can be quite cumbersome.

A very nice approach is using boost facade iterators, you just need to rewrite what is needed that is three methods for an input iterator that specify how to increment, test if two iterators are equals and dereference. Boost is then doing all the dirty work for you and you can then use all stl algorithms with your standard compliant iterators.

Here is an example from the link I gave you :

# include <boost/iterator/iterator_facade.hpp>
# include "node.hpp"
class node_iterator  : public boost::iterator_facade<
    node_iterator
  , node_base
  , boost::forward_traversal_tag
>{
 public:
node_iterator()
  : m_node(0) {}

explicit node_iterator(node_base* p)
  : m_node(p) {}
 private:
friend class boost::iterator_core_access;

void increment() { m_node = m_node->next(); }

bool equal(node_iterator const& other) const
{
    return this->m_node == other.m_node;
}

node_base& dereference() const { return *m_node; }

node_base* m_node;
};

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254431

The keys of a map are constant, so the value type is pair<const string, Word>.

Some type aliases might make the code friendlier:

typedef map <string,Word> map_type;
typedef map_type::value_type value_type;

value_type &operator*();

Upvotes: 9

Related Questions