470,620 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

std::list<T>::iterator

Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:

template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
void remove(const T& value) { lst->remove(value); }
};

Jun 27 '08 #1
17 3857
On 2008-05-22 13:02:39 -0400, Isliguezze <is********@gmail.comsaid:
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:
What is the purpose of this class?
>
template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
void remove(const T& value) { lst->remove(value); }
};

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Jun 27 '08 #2
Isliguezze wrote:
Does anybody know how to make a wrapper for that iterator?
What for?
Here's my
wrapper class for std::list:

template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
The assignment op is missing (see Rule of Three).
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
void remove(const T& value) { lst->remove(value); }
};
This wrapper seems awfully thin and doesn't provide any additional
functionality beyond what's already available in std::list. What do you
have in mind when creating a wrapper like this? Perhaps if you could
explain your design goals it would be easier to suggest a solution for
the iterator...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #3
What is the purpose of this class?
Good question. It is kind of "I want to understand STL a little bit
better". You know, I have to implement a "list" anyhow. So I decided
write it using STL. Also, as I told before, I want to know how all
this is cooked.

Jun 27 '08 #4
Frankly speaking, I have to add std::list::insert function to the
wrapper and I don't know how to do it without iterators. Also, I want
to have own iterators to let user to iterate (I mean to do for
(List<T>::Itr i = L.begin(); ... etc)
Jun 27 '08 #5
Isliguezze a écrit :
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:
(I assume that you don't wrap just for the sake of wrapping, and that
the added value of your wrapper is hiden for the purpose of your question.)
template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
These two line most probably don't do what you think: you don't call
std::list<T>::pop_back, you just write an expression evaluating to the
address of this function. Do not forget the '()'.
void remove(const T& value) { lst->remove(value); }
};
Well, for the iterator, you could add a class Iterator { ... }; in the
definition of your List.

With the same level of added value, this Iterator class would have a
member data named itr, and you would implement a forwarding function for
each function of std::list<T>::iterator, as you did for list.

I hope it helps,
--
Vincent Jacques

"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
Devise Shadock
Jun 27 '08 #6
Isliguezze a écrit :
>What is the purpose of this class?
You know, I have to implement a "list" anyhow.
No offense, but you're not *implementing* a list. You're adding a layer
on top of the existing (and well implemented) std::list.

I mean, if you are implementing a list as an exercise, it would be much
better to really implement it, with the double-linked cells (the teacher
will not be happy with the forwarding version, and you will have learned
only a few things)

I you want to *use* you List, why not use std::list directly?

Cheers,
--
Vincent Jacques

"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème"
Devise Shadock
Jun 27 '08 #7
Actually I didn't really understand how to make own version of the
iterator... make a new class Iterator and use it as a friend classof
what? How to do so that a user could just write something like this:
List<Tlst;
for (List<T>::It i = lst.begin(); i != lst.end(); ++i) {
//...
}
?
Jun 27 '08 #8
In article
<10**********************************@z72g2000hsb. googlegroups.com>,
Isliguezze <is********@gmail.comwrote:
Does anybody know how to make a wrapper for that iterator? Here's my
wrapper class for std::list:

template <class Tclass List {
private:
std::list<T*lst;
public:
List() { lst = new std::list<T>(); }
List(const List<T&rhs) { lst = new std::list<T>(*rhs.lst); }
List(int n, const T& value) { lst = new std::list<T>(n, value); }
~List() { delete lst; }

void push_back(const T& value) { lst->push_back(value); }
void push_front(const T& value) { lst->push_front(value); }
void pop_back() { lst->pop_back; }
void pop_front() { lst->pop_front; }
void remove(const T& value) { lst->remove(value); }
};
First, don't contain the std::list by pointer.

template < typename T >
class List
{
std::list<Trep;
public:
typedef typename std::list<T>::iterator iterator;

List():rep() { }
List( int n, const T& value ): rep( n, value ) { }

void push_back( const T& value ) { rep.push_back( value ); }
// etc...

iterator begin() { return rep.begin(); }
};
Jun 27 '08 #9
Hi!

Daniel T. schrieb:
First, don't contain the std::list by pointer.
Which is indeed a good start to whatever the original poster wants to
achieve. It removes the various memory leaks by doing automatic memory
management (which so many people claim C++ wouldn't have ;) ).

And, yes, Daniel shows the simple "reuse the iterator" solution:
public:
typedef typename std::list<T>::iterator iterator;
[snip]
iterator begin() { return rep.begin(); }
};
You better get this working first, before implementing an own iterator.

Regards,
Frank
Jun 27 '08 #10
But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
Jun 27 '08 #11
Isliguezze <is********@gmail.comwrote:
But gentlemen, if I do not use pointers, how do I make the destructor?
You don't need to.
Is this correct: ~List() { rep.~list(); } ?
No. The correct d_tor would be: ~List() { }
Jun 27 '08 #12
But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
Jun 27 '08 #13
But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
Jun 27 '08 #14
Daniel T. schrieb:
Isliguezze <is********@gmail.comwrote:
>Is this correct: ~List() { rep.~list(); } ?

No. The correct d_tor would be: ~List() { }
Following functionality would automatically be correct, meaning
"out-of-the-box" ready:

- Copy Constructor
- Assignment operator
- Destructor

That means you write no code, but have full functionality!!

This standard behaviour works like these functions:

template<typename T>
class List
{
std::list<Tlst;
public:
List(List<Tconst& original)
: lst(original.lst)
{}

List<T>& operator = (List<Tconst& original)
{
lst = original.lst;
return *this;
}

~List() {}
};

But you don't even need to write them!!

Regards, Frank
Jun 27 '08 #15
On May 23, 2:24*pm, Isliguezze <isligue...@gmail.comwrote:
But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
Unless you need to manually call the dTor the wrapper will call the
member's dTor automatically.
It's like you dont always ->need<- to call the default cTor.

Try this link - It's a great ->free<- book for newbies like us.
http://www.linuxtopia.org/online_boo...mming_173.html
Jun 27 '08 #16
In article <2d5b7bc8-7161-4da6-b8c4-
70**********@k13g2000hse.googlegroups.com>, is********@gmail.com says...
But gentlemen, if I do not use pointers, how do I make the destructor?
Is this correct: ~List() { rep.~list(); } ?
No. The dtor you included in your original post matched up correctly
with the ctors you included.

You typically only use the direct dtor call syntax like above in
conjunction with a placement new operator.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #17
On Thu, 22 May 2008 10:44:59 -0700 (PDT), Isliguezze wrote:
>Actually I didn't really understand how to make own version of the
iterator... make a new class Iterator and use it as a friend classof
what? How to do so that a user could just write something like this:
List<Tlst;
for (List<T>::It i = lst.begin(); i != lst.end(); ++i) {
//...
}
See: http://cpphelp.com/slist/
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jun 27 '08 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ishekar | last post: by
14 posts views Thread by Dave | last post: by
2 posts views Thread by Christoph Heindl | last post: by
3 posts views Thread by banangroda | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.