473,394 Members | 2,100 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,394 software developers and data experts.

-- myList.begin()

Is that operation legal does anyone know?

Can I legally compare an iterator to the thing just before begin to see
if decrementing my iterator has taken it past the workable range of the
list?

If not, what should I be doing instead to terminate this loop ...

while(-- it != /* hmm */)
{
// play with it->
}

Oct 20 '06 #1
6 1468

earthwormgaz wrote:
Is that operation legal does anyone know?

Can I legally compare an iterator to the thing just before begin to see
if decrementing my iterator has taken it past the workable range of the
list?
No. That would be undefined behaviour.
If not, what should I be doing instead to terminate this loop ...

while(-- it != /* hmm */)
{
// play with it->
}
If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.

Gavin Deane

Oct 20 '06 #2

Gavin Deane wrote:
No. That would be undefined behaviour.
Right, I'll stop doing that then. It actually has worked for me, but is
clearly an unsafe thing to do, so that is the end of it.
If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.
I am using a list, so yes, random access iterator. It would seem that
all I can do is ...

-- it != myList.begin()

I'll have to rework a couple of algorithms for that I suppose.

Gaz

Oct 20 '06 #3
earthwormgaz wrote:
>
Gavin Deane wrote:
>No. That would be undefined behaviour.

Right, I'll stop doing that then. It actually has worked for me, but is
clearly an unsafe thing to do, so that is the end of it.
>If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.

I am using a list, so yes, random access iterator.
list iterators are not random access. They are only bidirectional iterators.
It would seem that all I can do is ...

-- it != myList.begin()

I'll have to rework a couple of algorithms for that I suppose.
What's wrong with reverse iterators? Use rbegin()/rend() to go through the
container backwards.

Oct 20 '06 #4
earthwormgaz wrote:
Gavin Deane wrote:
>No. That would be undefined behaviour.

Right, I'll stop doing that then. It actually has worked for me, but is
clearly an unsafe thing to do, so that is the end of it.
>If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.

I am using a list, so yes, random access iterator.
No, it's not. List does not have random access iterators.
It would seem that
all I can do is ...

-- it != myList.begin()

I'll have to rework a couple of algorithms for that I suppose.
Did you read Gavin Deane's post? Use reverse iterators.
--
Clark S. Cox III
cl*******@gmail.com
Oct 20 '06 #5

Rolf Magnus wrote:
What's wrong with reverse iterators? Use rbegin()/rend() to go through the
container backwards.
Nowt, but I am writing a bi-directional iterator for what amounts to
this ...

class Tree
{
std::list<Treem_lsChildren;
};

So, my iterator type contains:

class TreeIterator
{
Tree * m_pTree;
std::list<Tree>::iterator m_pIt;
};

That means in dealing with the -- operator I have to deal with m_pIt
going past begin(). I appear to have a working algorithm now anyway.

/me waits for somebody to tell him Boost or something already does all
this :)

Cheers folks,

Gaz

Oct 20 '06 #6

earthwormgaz wrote:
Gavin Deane wrote:
No. That would be undefined behaviour.

Right, I'll stop doing that then. It actually has worked for me, but is
clearly an unsafe thing to do, so that is the end of it.
If you are using a container with bidirectional or random access
iterators (you mention the word list above and std::list has
bidirectional iterators) then look up the reverse iterators rbegin()
and rend(). They allow you to iterate backwards through the container.
It's not absolutely clear from your question, but it sounds like they
might be what you need.

I am using a list, so yes, random access iterator. It would seem that
all I can do is ...

-- it != myList.begin()

I'll have to rework a couple of algorithms for that I suppose.

Gaz
A container with bidirerctional iterators has 2 ends, one end gowing
forward indicating one past the last element and a rend gowing backward
indicating one past the first element.

#include <iostream>
#include <list>

int main()
{
std::list<intnlist;
for(int i = 0; i < 5; ++i)
{
nlist.push_back(i); // 0,1,2,3,4
}

typedef std::list<int>::reverse_iterator RIter;
for(RIter riter = nlist.rbegin(); riter != nlist.rend(); ++riter)
{
std::cout << *riter << std::endl;
}

return 0;
}

/*
4
3
2
1
0
*/

Note how incrementing (op++) a reverse iterator is used to displace it.

Oct 20 '06 #7

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

Similar topics

1
by: Steve Thorpe | last post by:
Hi have have two linked SQL Servers and I am trying to get things working smootly/quickly. Should I be using 'BEGIN TRANSACTION' or 'BEGIN DISTRIBUTED TRANSACTION' ? Basicly, these SPs update...
11
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
2
by: gabriel | last post by:
Greetings, I am adding foreign keys to a database and saving the generated scripts. What I do not understand is that all script begin with empty transactions. Why ? Example follows : /*
2
by: Nick Keighley | last post by:
Hi, I'm trying to iterate over a container class. The class actually contains a std::map, so I thought all I'd have to implement begin() and end(). The example code has a problem in begin() ...
1
by: Karl O. Pinc | last post by:
FYI, mostly. But I do have questions as to how to write code that will continue to work in subsequent postgresql versions. See code below. begintest() uses EXIT to exit a BEGIN block from...
2
by: serge | last post by:
/* Subject: How best to use BETWEEN Begin and End Dates to find out if an employee was/is member of any group for a certain date range? You can copy/paste this whole post in SQL Query Analyzer...
11
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However,...
1
by: Xoomer | last post by:
How to out everything from TStringList MyList? Example: TStringList* MyList = new TStringList(); MyList->Add("1..."); MyList->Add("Two....."); MyList->Add("this is three"); MyList->Add("now...
3
by: imran akhtar | last post by:
i have to Write a program which outputs the alternating sum of all elements of a list taken in as a parameter. For example if myList contains 4, 7, 9, 23, 45, 12, the function should output the...
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: 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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.