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 5 1874
Thomas Matthews wrote: 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.
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(?????);
template <typename Iter>
void display_references(Iter begin, Iter end);
};
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 wrote in news:I8mEb.37689$Kh6.35485
@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
{
template < typename Iter >
void display_references( Iter ptr, Iter Lim );
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.
-- http://www.victim-prime.dsl.pipex.com/
Thomas Matthews wrote: 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
};
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.
Jeffrey Schwab wrote: Thomas Matthews wrote:
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.
My base class is "Reference". Subclasses are Magazine, Book, etc.
Nothing to do with C++ references or proxies. 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
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
Thomas Matthews wrote: Jeffrey Schwab wrote:
Thomas Matthews wrote:
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.
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." 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Grant Edwards |
last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273
Alan Kay said something I really liked, and I think it applies
equally well to Python as well as the languages...
|
by: Brett L. Moore |
last post by:
Hi,
I have an octree class that I would like to (slowly) migrate to an
STL-like container. I now need to implement two forward iterators for the
class. I would like to use the iterators in the...
|
by: Markus Dehmann |
last post by:
I have a circular dependency between two classes.
The FAQ hint about a forward declaration does not help in my case
( How can I create two classes that both know about each
other?)
Error:...
|
by: Jim Langston |
last post by:
I have a class I designed that stores text chat in a
std::vector<sd::string>. This class has a few methods to retrieve these
strings to be displayed on the screen.
void ResetRead( bool Reverse,...
|
by: Mark Stijnman |
last post by:
I have a question about forward iterators and what one should do or not
do with them. I'm planning on writing a container that, when all boils
down to it, stores a sequence of strings. I want...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
|
by: Steven D'Aprano |
last post by:
I thought that an iterator was any object that follows the iterator
protocol, that is, it has a next() method and an __iter__() method.
But I'm having problems writing a class that acts as an...
|
by: Lambda |
last post by:
Hi, All,
Why the search() function use Forward Iterator?
I think Input Iterator is enough, I need read only.
In such kind of situations, how should I choose iterator type?
|
by: mickey0 |
last post by:
hello, I wrote this but it doens't compile; more or less I see the error; I have to include "pattern.h" instead of "class Pattern" in patternset.h; But I don't understand why:
This is the error:
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |