473,387 Members | 1,579 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,387 software developers and data experts.

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 4130
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ishekar | last post by:
Hi all, Dont hit me for asking this in this forum but i feel this question is related to allocators hence i think its the right place. I was trying to use the list of CComBSTR but was getting...
14
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
2
by: Christoph Heindl | last post by:
Hi, I'm currently struggling with a design decision. One of my classes (named Attributable) has a protected member of type std::list<StrongReference<Attribute> > Basically the list has...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
3
by: janzon | last post by:
Hi! Sorry for the bad subject line... Here's what I mean. Suppose we deal with C++ standard integers lists (the type is indifferent). We have a function f, declared as list<intf(int); Now...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
10
by: arnuld | last post by:
It is quite an ugly hack but it is all I am able to come up with for now :-( and it does the requires work. I want to improve the program, I know you people have much better ideas ;-) /* C++...
3
by: banangroda | last post by:
Compilation fails at "line.insert(line.end(), x.begin(), i);" and I can't figure out why. Here is the code: /* 5-1. Design and implement a program to produce a permuted index. A permuted index...
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: 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:
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.