A Forward Iterator type / class?

T

Thomas Matthews

Hi,

I have a Display class. I would like to write a function that takes a
range of objects and displays them. The range would be specified by
two forward iterators: start and end (one past start).

I created a base class "References" to test this concept. I want
the display function to process either a vector<References> or
a list<References>. However, in my compiler (Borland C++ Builder),
the std::list has a different iterator type than vector.

So how can I write a method to process a range of objects,
regardless of the container (assume that the fundamental requirement
for a range is forward iteration)?

struct Reference
{
string get_category(void) const;
string get_title(void) const;
};

class User_Interface
// : public Singleton<User_Interface>
// i.e. User_Interface is a Singleton
{
void display_references(?????);
};

User_Interface My_UI;

int main(void)
{
std::list<Reference> ref_list;
std::vector<Reference> ref_vector;

My_UI.display_references(ref_list.begin(),
ref_list.end());
My_UI.display_references(ref_vector.begin(),
ref_vector.end());
}

The above code is for illustrative purposes only and is
not meant to be compiled or run without errors.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Rolf Magnus

Thomas said:
Hi,

I have a Display class. I would like to write a function that takes a
range of objects and displays them. The range would be specified by
two forward iterators: start and end (one past start).

Didn't you mean one past _end_?
I created a base class "References" to test this concept. I want
the display function to process either a vector<References> or
a list<References>. However, in my compiler (Borland C++ Builder),
the std::list has a different iterator type than vector.

That's not surprising.
So how can I write a method to process a range of objects,
regardless of the container (assume that the fundamental requirement
for a range is forward iteration)?

Use templates.
 
J

Jeffrey Schwab

Thomas said:
Hi,

I have a Display class. I would like to write a function that takes a
range of objects and displays them. The range would be specified by
two forward iterators: start and end (one past start).

I created a base class "References" to test this concept. I want

That's a potentially confusing choice of name. "Reference" already
means something completely different. Consider "Proxy" instead.
the display function to process either a vector<References> or
a list<References>. However, in my compiler (Borland C++ Builder),
the std::list has a different iterator type than vector.

So how can I write a method to process a range of objects,
regardless of the container (assume that the fundamental requirement
for a range is forward iteration)?

struct Reference
{
string get_category(void) const;
string get_title(void) const;
};

class User_Interface
// : public Singleton<User_Interface>
// i.e. User_Interface is a Singleton
{
void display_references(?????);

The answer to the question you asked, is: Make the type of the
iterators a template parameter. E.g.:

template< typename For /* forward iterator */ >
void display_references( For p, For const& end )
{
// Do your thing...
}

The answer to the question you didn't ask is: Don't reinvent the loop.
Use std::for_each. E.g.:

std::for_each( list.begin( ), list.end( ), display );

std::for_each( vector.begin( ), vector.end ), display );

Hth,
Jeff
 
R

Rob Williscroft

Thomas Matthews wrote in @newssvr32.news.prodigy.com:

[sniping...]
.... However, in my compiler (Borland C++ Builder),
the std::list has a different iterator type than vector.

So how can I write a method to process a range of objects,
regardless of the container (assume that the fundamental requirement
for a range is forward iteration)?

Use a template member function:
struct Reference
{
};

class User_Interface


void display_references(?????);
};
template < typename Iter >
void User_Interface::display_references( Iter ptr, Iter Lim )
{

}
User_Interface My_UI;

int main(void)
{
My_UI.display_references(ref_list.begin(),
ref_list.end());
My_UI.display_references(ref_vector.begin(),
ref_vector.end());
}

The above code is for illustrative purposes only and is
not meant to be compiled or run without errors.

Rob.
 
T

Thomas Matthews

Jeffrey said:
That's a potentially confusing choice of name. "Reference" already
means something completely different. Consider "Proxy" instead.
My base class is "Reference". Subclasses are Magazine, Book, etc.
Nothing to do with C++ references or proxies.

The answer to the question you asked, is: Make the type of the
iterators a template parameter. E.g.:

template< typename For /* forward iterator */ >
void display_references( For p, For const& end )
{
// Do your thing...
}

The answer to the question you didn't ask is: Don't reinvent the loop.
Use std::for_each. E.g.:

std::for_each( list.begin( ), list.end( ), display );

std::for_each( vector.begin( ), vector.end ), display );

Hth,
Jeff
Personally, I don't see any gain in efficiency or readability with
the std::for_each algorithm; but that is my opinion.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
J

Jeffrey Schwab

Thomas said:
My base class is "Reference". Subclasses are Magazine, Book, etc.
Nothing to do with C++ references or proxies.

See how misleading the name was? :)

The nature of the class might be clear from context to anyone reading
your complete program, but I still think you have to be the kind of
person who kicks puppies to name a class "Reference."
Personally, I don't see any gain in efficiency or readability with
the std::for_each algorithm; but that is my opinion.

Well, how about the fact that it would have obviated your original post
entirely, and you never would have had the any problem using multiple
types of iterators? Or, how about the fact that for_each saves you from
having to write the looping code? Or, how about the fact that the
program is shorter? Or... maybe we can just agree to disagree.

-Jeff
 

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,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top