473,714 Members | 2,490 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL list::iterator problem

Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or
{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?

The reason I derived MyIterator from list<T>::iterat or is that I want
to support all the overloaded operator that list<T>::iterat or supports.

Thanks
Jayesh Shah

Nov 24 '06 #1
15 2793
Hi

ja*****@gmail.c om wrote:
List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
What is the difference? MyIterator will need to be some template, as
otherwise you cannot distinguish different element types. So it would
probably be MyIterator<int> . I can't see any difference to
list<int>::iter ator.

Markus

Nov 24 '06 #2
ja*****@gmail.c om wrote:
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
Why? Looks like a BadIdea(tm) to me.
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or
Note: there is no guarantee that list<T>::iterat or is inheritable. However,
this will not be a problem in practice since, as far as I know, all widely
used implementations implement list<>::iterato r as a class without
finalizing trickery.

{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};
You need to construct the base:

MyIterator ( list<T& mylist )
: list<T>::iterat or( mylist.begin() )
{}

(untested)
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}
And how do you intend to use these iterators in a loop:

MyIterator itr ( mylist );
while ( itr != ??? ) {
...
++itr;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?
Yes: use mylist.begin() and mylist.end().

[snip]
Best

Kai-Uwe Bux
Nov 24 '06 #3
On Nov 24, 1:34 pm, jaye...@gmail.c om wrote:
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
I'll admit that I have not tested this but I'm quite sure that
STL-iterators have a copy-constructor so something like this ought to
work:

#include <list>
#include <iostream>

typedef MyList std::list<int>
typedef MyIterator std::list<int>: :iterator

MyList l;
MyIterator itr(l.begin());
std::cout << *itr;
>template <class Tclass MyIterator : public list<T>::iterat or
{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};

};
If you realy want to do it your way I think you need to initialize the
base of your derived class like this (again, untested):

#include <list>
#include <iostream>

template <class Tclass MyIterator : public list<T>::iterat or
{
MyIterator (std::list<T>& l)
: std::list<T>::i terator(l.begin ())
{ };
};

int main()
{
std::list<intl;
l.push_back(2);
MyIterator<inti ter(l);
std::cout << *iter;
return 0;
}

Notice the template parameter, like Markus pointed out, when creating
an instance of the MyIterator-class.

--
Erik Wikström

Nov 24 '06 #4
In article <11************ **********@l12g 2000cwl.googleg roups.com>,
ja*****@gmail.c om wrote:
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
Why? What problem does this solve? I can understand writing algorithms
that take whole containers rather than 'begin' and 'end' but this? I
don't get it.
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or
{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?

The reason I derived MyIterator from list<T>::iterat or is that I want
to support all the overloaded operator that list<T>::iterat or supports.
What about all the other iterators? Anything you write in MyIterator
should work just as well with any other bi-directional iterator yet you
only derived from list<T>::iterat or. That seems quite limiting.

I have an idea though...

template < typename Con >
class MyIterator
{
Con& container;
typename Con::iterator pos;
public:
typedef typename Con::reference reference;

MyIterator( Con& c ): container( c ), pos( c.begin() ) { }

reference operator*() {
return *pos;
}
};

int main() {
list<intmyList;
MyIterator<list <int itr( myList );
cout << (*itr);
}

--
To send me email, put "sheltie" in the subject.
Nov 24 '06 #5
<ja*****@gmail. comwrote in message
news:11******** **************@ l12g2000cwl.goo glegroups.com.. .
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or
{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?

The reason I derived MyIterator from list<T>::iterat or is that I want
to support all the overloaded operator that list<T>::iterat or supports.

Thanks
Jayesh Shah
I think the problem you're going to run into is that mylist really doesn't
know what it is. That is, you really want:
MyIterator iter(mylist);
to expand to
MyIterator iter(list<int>) ;
or the like (although that above won't compile I"m sure).

I understand why you want to do this, and it would be nice, but I really
don't think it's possible The way I deal with it is by using typedef.

typedef list<intDataLis t;

DataList MyList;
DataList::itera tor it = MyList.begin();
std::cout << (*itr);

I don't really think you can generically create an iterator off a generic
container. It would be nice if you could, but still, what if it's a map?
Then you have .first() and .second() which a list doesn't have, etc...

Nov 24 '06 #6

Kai-Uwe Bux wrote:
ja*****@gmail.c om wrote:
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);

Why? Looks like a BadIdea(tm) to me.
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or

Note: there is no guarantee that list<T>::iterat or is inheritable. However,
this will not be a problem in practice since, as far as I know, all widely
used implementations implement list<>::iterato r as a class without
finalizing trickery.

{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};

You need to construct the base:

MyIterator ( list<T& mylist )
: list<T>::iterat or( mylist.begin() )
{}

(untested)
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}

And how do you intend to use these iterators in a loop:

MyIterator itr ( mylist );
while ( itr != ??? ) {
...
++itr;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?

Yes: use mylist.begin() and mylist.end().

[snip]
Best

Kai-Uwe Bux
Thanks a lot Bux.
You said there is no guarantee that list<T>::iterat or is inheritable.
Is it defined in spec ?
How come you implement a iterator without the class since you have lots
of operator to overload ?

Nov 25 '06 #7
ja*****@gmail.c om wrote:

Thanks a lot Bux.
You said there is no guarantee that list<T>::iterat or is inheritable.
Is it defined in spec ?
By "spec" you mean the C++ standard? No, it's not defined in the standard.
That's why there is no guarantee. The type list<T>::iterat or is marked
as "implementa tion defined".

For std::vector<T>, for instance, an implementation is free to use T* as an
iterator. In that case, std::vector<T>: :iterator would not be a class.

For std::list<T>, an implementation is free to use trickery (using virtual
base classes) to prevent inheritance. (Although, to my knowledge, no
implementation actually does that.)

How come you implement a iterator without the class since you have lots
of operator to overload ?
Huh? I do not understand, and I think that sentence just does not parse. I
am not a native speaker of English, so my parser has little tolerance and
is very limited with regard to error correction.
Best

Kai-Uwe Bux
Nov 25 '06 #8

Daniel T. wrote:
In article <11************ **********@l12g 2000cwl.googleg roups.com>,
ja*****@gmail.c om wrote:
Hi All,

List and its iterator work as following way :

list<intmylist;
list<int>::iter ator itr;
itr = mylist.begin();
cout << (*itr);

But I want something like this:

list<intmylist;
MyIterator itr(mylist);
cout<< (*itr);

Why? What problem does this solve? I can understand writing algorithms
that take whole containers rather than 'begin' and 'end' but this? I
don't get it.
So I wrote MyIterator following way :

template <class Tclass MyIterator : public list<T>::iterat or
{
public:
MyIterator() { };

~MyIterator() { };

MyIterator(list <T>& mylist) {

/* What I should write here */
};
};

int main()
{
list<intmylist;
mylist.push_bac k(1);
mylist.push_bac k(2);

/* I want to support following construct */

MyIterator iter(mylist);
cout <<*mylist;

return 0;
}

Can anybody please guide me what I should write in one parameter
constructor ? Or do you have all together different solution ?

The reason I derived MyIterator from list<T>::iterat or is that I want
to support all the overloaded operator that list<T>::iterat or supports.

What about all the other iterators? Anything you write in MyIterator
should work just as well with any other bi-directional iterator yet you
only derived from list<T>::iterat or. That seems quite limiting.

I have an idea though...

template < typename Con >
class MyIterator
{
Con& container;
typename Con::iterator pos;
public:
typedef typename Con::reference reference;

MyIterator( Con& c ): container( c ), pos( c.begin() ) { }

reference operator*() {
return *pos;
}
};

int main() {
list<intmyList;
MyIterator<list <int itr( myList );
cout << (*itr);
}

--
To send me email, put "sheltie" in the subject.

Absolutely a brilliant solution.....

You suggested the usage as :
MyIterator<list <int itr( myList );

But I want to use as

MySlistIterator itr(list); // I can not change this usage

So what I thought is

template<class Ttypedef MyIterator<list < T MySlisIterator< T>;

but this is not supported in c++..what can be the alternative ??

Thanks a lot again..........

Nov 25 '06 #9
ja*****@gmail.c om wrote:
You suggested the usage as :
MyIterator<list <int itr( myList );

But I want to use as

MySlistIterator itr(list); // I can not change this usage
Why? What problem does this solve?

--
To send me email, put "sheltie" in the subject.
Nov 25 '06 #10

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

Similar topics

7
4341
by: sam | last post by:
Hi, This is a "list iterator" problem I expect it will copy the list iterator (l_iter) to the caller: eg. list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i) { list<HashMap>::iterator l_iter; HashMap::iterator m_iter; for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
15
19738
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value to a std::list iterator variable (or is it better to use a seperate bool variable as a "null flag" ?) e.g. struct mystru { std::string nm;
5
22832
by: gerg | last post by:
I'm having to deal with some legacy code which I think may be causeing some problems. Can STL pro's please add comments about the correctness of the following code: class A { public: /// ... assume constructors and such are defined char* sz; // pointer to memory buffer };
8
16948
by: freckred76 | last post by:
Hi, I think this might be a VC++ problem. I am using Microsoft Visual Studio 2005 Full Version (8.0). I have a simple for loop that iterates over a list using the standard iterator. The list is populated with a struct _BLOCK defined as: typedef struct {
3
23514
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:818044 Hi, I am wondering if I can assign a list iterator = NULL. Suppose I have a class A: A.h class A{ //ctors etc etc.
6
3715
by: Jason S | last post by:
where is the behavior of list::iterator specified? I need to keep track of ranges of data, all I can figure out so far is that iterators are guaranteed to be valid unless you remove the elements they point to. Example -- if L is a list and Li = L.end(), and you call L.push_back(something), how do you know whether Li will still point to the list's end, or whether it will point to the something? I tried this program in Microsoft VC++ 6.0...
2
3671
by: Rohit.MEHTA | last post by:
Hi All I want to overload the ++ and - operator for stl list iterator so that I could do some thing additional in that As want to set the current node status also Can some one please post sample code to use this, I would NOT like to do it by inheriting the iterator class. Rather I would like to keep a member iterator and wrap the logic around it
1
1534
by: vishnu vardhan K S | last post by:
Hi, I couldn't use List iterator in side a constant function. if i use like this given below Buffer& ArqFeedbackMessage::encodeArqFeedbackMessage() const { Buffer* encodedMsg = new Buffer(); *encodedMsg << mmt; list<ArqFeedBackIE>::iterator i;
1
6239
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party library from the higher level classes. This low level class is used to allocate locked pages of memory before doing a DMA. The third party library locks memory and returns a pointer to an internal structure, which we can simply cast to a (void...
0
8795
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8701
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9168
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7942
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6621
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5943
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2103
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.