473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

A Forward Iterator type / class?

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

Jul 22 '05 #1
5 1897
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.


Jul 22 '05 #2
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/
Jul 22 '05 #3
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.


Jul 22 '05 #4
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

Jul 22 '05 #5
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

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
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...
3
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...
6
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:...
8
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,...
5
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...
0
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...
27
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...
1
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?
7
mickey0
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: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.