Special map iterator

B

Bobo

Hello, all.
I have just wrote the following little class, and I'd like you to give
me your opinion about it. I compiles with VC6 but I'm not sure if it
is totally right.

I'm trying to explain the problem I try to solve: I had a class with a
private vector<T> member variable, and some public functions that use
iterators to the vector (with the apropiate public typedef for the
iterator type). But now I need to change the vector to a map. That's
easy. But the iterator iterates now with a std::pair<I,T> type. And of
course I want to maintain the public interface.

template <class M>
class iterator_second : public M::iterator
{
public:
typedef typename M::iterator iterator_base;
typedef typename M::referent_type reference_type;
typedef reference_type *pointer_type;
iterator_second()
{}
iterator_second(iterator_base &it)
:iterator_base(it)
{}
reference_type &operator *()
{
return iterator_base::eek:perator*().second;
}
pointer_type &operator ->()
{
return &iterator_base::eek:perator->()->second;
}
};

// iterator_second<std::map<I,T> > iterates through the T values of
the map.

TIA.
Bobo.
 
R

Rob Williscroft

Bobo wrote in
Hello, all.
I have just wrote the following little class, and I'd like you to give
me your opinion about it. I compiles with VC6 but I'm not sure if it
is totally right.

I'm trying to explain the problem I try to solve: I had a class with a
private vector<T> member variable, and some public functions that use
iterators to the vector (with the apropiate public typedef for the
iterator type). But now I need to change the vector to a map. That's
easy. But the iterator iterates now with a std::pair<I,T> type. And of
course I want to maintain the public interface.

#include <iostream>
#include <string>
#include <map>

template <class M>
class iterator_second : public M::iterator
{
public:
typedef typename M::iterator iterator_base;

/* In most std containers the reference and pionter typedefs
are called 'reference' and 'pointer' not 'reference_type'.
I would prefere reference_type etc just for consistancy
withe value_type etc.

Also these are refrences/ponters so reference &f() would be
illegal ( i.e some_type & &f() ).

I tried second_type first, had to hit Ctrl-F1 to find out it
should be 'mapped_type' (didn't check the Standard)
*/
typedef typename M::mapped_type &reference;
typedef typename M::mapped_type *pointer;

iterator_second()
{}

/* needs to take a const & argument to allow conversion from
map<>::begin() and end().
*/
iterator_second(iterator_base const &it)
:iterator_base(it)
{}

/* Note lack of reference &.
These can also be const as they don't alter this onstance.
*/
reference operator *() const
{
return iterator_base::eek:perator*().second;
}
pointer operator ->() const
{
return &iterator_base::eek:perator->()->second;
}
};

/* A simple test
*/
int main()
{
using namespace std;

map< int, string > m;
m[1] = "aaa";
m[2] = "bbb";

iterator_second< map< int, string > > ptr, lim;

ptr = m.begin();
lim = m.end();

for (; ptr !=lim; ++ptr )
{
cerr << *ptr << '\n';
}
}

Rob.
 
B

Bobo

Rob Williscroft said:
Bobo wrote in

#include <iostream>
#include <string>
#include <map>

template <class M>
class iterator_second : public M::iterator
{
public:
typedef typename M::iterator iterator_base;

/* In most std containers the reference and pionter typedefs
are called 'reference' and 'pointer' not 'reference_type'.
I would prefere reference_type etc just for consistancy
withe value_type etc.

Also these are refrences/ponters so reference &f() would be
illegal ( i.e some_type & &f() ).

Ops! I didn't even noticed that!
I tried second_type first, had to hit Ctrl-F1 to find out it
should be 'mapped_type' (didn't check the Standard)

Yes, I have just checked it, it's right.
*/
typedef typename M::mapped_type &reference;
typedef typename M::mapped_type *pointer;

iterator_second()
{}

/* needs to take a const & argument to allow conversion from
map<>::begin() and end().
*/
iterator_second(iterator_base const &it)
:iterator_base(it)
{}
Ah! The reference bound to temporary trick...
/* Note lack of reference &.
These can also be const as they don't alter this onstance.
*/
reference operator *() const
{
return iterator_base::eek:perator*().second;
}
pointer operator ->() const
{
return &iterator_base::eek:perator->()->second;
}
};

Thank you very much!

Bobo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top