473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with iterators

1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is undefined?
I thought that I had to decrement myset.end() by one to get the value 4.

2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators while set supports
bidirectional iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?
Jun 7 '07 #1
18 2095
desktop wrote:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
This causes undefined behaviour. The iterator returned by 'end()' is
non-dereferenceable . You probably want '*myset.rbegin( )'.
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two
iterators: "it_p" and "it_q". When I print "first" and "second" I get
1 and 4. But should:

int second = *myset.end();

not return the value of the element *after* the last which is
undefined? I thought that I had to decrement myset.end() by one to
get the value 4.
Whatever you think your program should do, it's all wrong. The code
has undefined behaviour.
2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators
Wrong. std::list supports bidirectional iterators.
while set supports
bidirectional iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?
Get a copy of the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 7 '07 #2
Victor Bazarov wrote:
desktop wrote:
>1)

I have this code:

std::list<intm ylist;
mylist.push_ba ck(1);
mylist.push_ba ck(2);
mylist.push_ba ck(3);
mylist.push_ba ck(4);

std::list<int> ::iterator it_p = mylist.begin();
std::list<int> ::iterator it_q = mylist.end();

std::set<intmy set(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();

This causes undefined behaviour. The iterator returned by 'end()' is
non-dereferenceable . You probably want '*myset.rbegin( )'.
>std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two
iterators: "it_p" and "it_q". When I print "first" and "second" I get
1 and 4. But should:

int second = *myset.end();

not return the value of the element *after* the last which is
undefined? I thought that I had to decrement myset.end() by one to
get the value 4.

Whatever you think your program should do, it's all wrong. The code
has undefined behaviour.
>2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators

Wrong. std::list supports bidirectional iterators.
Ok in the standard it says that list, vector and dequeue all are
Sequences. On page 469 it says that iterator should at least be forward.
But since binary minus '-' gives a compile error I assume they don't
support random access iterator. Is the *at least* formulation not a bit
un-precise?

Further the i and j iterators has to be of type input iterator. I don't
know of any containers that supports this type or any way that you can
make an input iterator.

Is it to make restrictions on the use of these iterators if they are
used in the eg. constructor (you are only allowed to use the operators
for input iterators even though no containers return this kind of iterator)?
Jun 7 '07 #3

desktop napsal:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is undefined?
I thought that I had to decrement myset.end() by one to get the value 4.
Yes, you have to do it. The code you've posted just invokes undefined
behaviour,
which means that it can result to anything the implementor decides,
particularly, it can return 4, but on a different platform, it might
crash the
program or return -45246961 or whatever else.
2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators while set supports
bidirectional iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?
Each container has a specific iterator class. This class satisfies
some
concept (similar to interface), theses are the Input/Forward/
Bidirectional/...
[btw. there is a hierarchy between these]. What each container's
iterator has
to satisfy, can be read in the SGI STL manual. For example, with list,
there is

list is a model of reversible container

and on a page about reversible container, there is

.... the iterators must be Bidirectional Iterators

so we know a list must hav an iterator at least as strong as
Bidirectional.

Regards
Jiri Palecek

Jun 7 '07 #4
desktop <ff*@sss.comwro te in news:f4******** **@news.net.uni-c.dk:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
Undefined behaviour. You cannot dereference an end() iterator.
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two
iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is
undefined?
I thought that I had to decrement myset.end() by one to get the value
4.

Undefined behaviour has been invoked. One can no longer predict the
behaviour.

Jun 7 '07 #5

desktop napsal:
Victor Bazarov wrote:
desktop wrote:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
This causes undefined behaviour. The iterator returned by 'end()' is
non-dereferenceable . You probably want '*myset.rbegin( )'.
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two
iterators: "it_p" and "it_q". When I print "first" and "second" I get
1 and 4. But should:

int second = *myset.end();

not return the value of the element *after* the last which is
undefined? I thought that I had to decrement myset.end() by one to
get the value 4.
Whatever you think your program should do, it's all wrong. The code
has undefined behaviour.
2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators
Wrong. std::list supports bidirectional iterators.

Ok in the standard it says that list, vector and dequeue all are
Sequences. On page 469 it says that iterator should at least be forward.
But since binary minus '-' gives a compile error I assume they don't
support random access iterator. Is the *at least* formulation not a bit
un-precise?
Did you get compile time error on list, deque or vector? list only
has
bidirectional iterator, whereas deque and vector are Random Access
Containers which support random access iterators.
>
Further the i and j iterators has to be of type input iterator. I don't
know of any containers that supports this type or any way that you can
make an input iterator.
Virtually any iterator you can make is input iterator. For example,
forward
iterator is a refinement of input iterator. So any iterator of any
container
will do. There are also "pure" input iterators like istream_iterato r.
Is it to make restrictions on the use of these iterators if they are
used in the eg. constructor (you are only allowed to use the operators
for input iterators even though no containers return this kind of iterator)?
Yes. This means you can use istream_iterato r in the constructor as
well.

Regards
Jiri Palecek

Jun 7 '07 #6
jp******@web.de wrote:
desktop napsal:
>1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is undefined?
I thought that I had to decrement myset.end() by one to get the value 4.

Yes, you have to do it. The code you've posted just invokes undefined
behaviour,
which means that it can result to anything the implementor decides,
particularly, it can return 4, but on a different platform, it might
crash the
program or return -45246961 or whatever else.
>2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators while set supports
bidirectiona l iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?

Each container has a specific iterator class. This class satisfies
some
concept (similar to interface), theses are the Input/Forward/
Bidirectional/...
[btw. there is a hierarchy between these]. What each container's
iterator has
to satisfy, can be read in the SGI STL manual. For example, with list,
there is

list is a model of reversible container

and on a page about reversible container, there is

.... the iterators must be Bidirectional Iterators

so we know a list must hav an iterator at least as strong as
Bidirectional.

Actually the C++ Standard says that i should be at least be forward see
page 469 item [5]:

http://www.usatlas.bnl.gov/~dladams/...14882-2003.pdf
Jun 7 '07 #7
jp******@web.de wrote:
desktop napsal:
>Victor Bazarov wrote:
>>desktop wrote:
1)

I have this code:

std::list<in tmylist;
mylist.push_ back(1);
mylist.push_ back(2);
mylist.push_ back(3);
mylist.push_ back(4);

std::list<in t>::iterator it_p = mylist.begin();
std::list<in t>::iterator it_q = mylist.end();

std::set<int myset(it_p,it_q );

int first = *myset.begin();
int second = *myset.end();
This causes undefined behaviour. The iterator returned by 'end()' is
non-dereferenceable . You probably want '*myset.rbegin( )'.

std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two
iterators: "it_p" and "it_q". When I print "first" and "second" I get
1 and 4. But should:

int second = *myset.end();

not return the value of the element *after* the last which is
undefined? I thought that I had to decrement myset.end() by one to
get the value 4.
Whatever you think your program should do, it's all wrong. The code
has undefined behaviour.

2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators
Wrong. std::list supports bidirectional iterators.
Ok in the standard it says that list, vector and dequeue all are
Sequences. On page 469 it says that iterator should at least be forward.
But since binary minus '-' gives a compile error I assume they don't
support random access iterator. Is the *at least* formulation not a bit
un-precise?

Did you get compile time error on list, deque or vector? list only
has
bidirectional iterator, whereas deque and vector are Random Access
Containers which support random access iterators.
>Further the i and j iterators has to be of type input iterator. I don't
know of any containers that supports this type or any way that you can
make an input iterator.

Virtually any iterator you can make is input iterator. For example,
forward
iterator is a refinement of input iterator. So any iterator of any
container
will do. There are also "pure" input iterators like istream_iterato r.
>Is it to make restrictions on the use of these iterators if they are
used in the eg. constructor (you are only allowed to use the operators
for input iterators even though no containers return this kind of iterator)?

Yes. This means you can use istream_iterato r in the constructor as
well.
What confuses me if its allowed use other operators on i and j besides
from the input iterator operations.

If I pass two vector iterators (random access) as i and j they fulfill
input operators but also some extra. Is it allowed use these extra
operators or is it only legal to use input defined ones?
Jun 7 '07 #8

desktop napsal:
jp******@web.de wrote:
desktop napsal:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is undefined?
I thought that I had to decrement myset.end() by one to get the value 4.
Yes, you have to do it. The code you've posted just invokes undefined
behaviour,
which means that it can result to anything the implementor decides,
particularly, it can return 4, but on a different platform, it might
crash the
program or return -45246961 or whatever else.
2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators while set supports
bidirectional iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?
Each container has a specific iterator class. This class satisfies
some
concept (similar to interface), theses are the Input/Forward/
Bidirectional/...
[btw. there is a hierarchy between these]. What each container's
iterator has
to satisfy, can be read in the SGI STL manual. For example, with list,
there is

list is a model of reversible container

and on a page about reversible container, there is

.... the iterators must be Bidirectional Iterators

so we know a list must hav an iterator at least as strong as
Bidirectional.


Actually the C++ Standard says that i should be at least be forward see
page 469 item [5]:

http://www.usatlas.bnl.gov/~dladams/...14882-2003.pdf
Yes, but in 23.2.2 item [2], on page 481, there is

A list satisfies all requirements ... of reversible container.

And in 23.1 item 9, there is that reversible containers have
Bidirectional or
Random Access category iterators. Because Random Access are stronger
than Bidirectional and Bidirectional are stronger than Forward (24.1),
we know that
a list have at leas as strong iterators as Bidirectional.

Jun 7 '07 #9
jp******@web.de wrote:
desktop napsal:
>jp******@web.de wrote:
>>desktop napsal:
1)

I have this code:

std::list<intmy list;
mylist.push_bac k(1);
mylist.push_bac k(2);
mylist.push_bac k(3);
mylist.push_bac k(4);

std::list<int>: :iterator it_p = mylist.begin();
std::list<int>: :iterator it_q = mylist.end();

std::set<intmys et(it_p,it_q);

int first = *myset.begin();
int second = *myset.end();
std::cout << "first = " << first << std::endl;
std::cout << "second = " << second << std::endl;
I copy the elements from the "list" to the "set" with the two iterators:
"it_p" and "it_q". When I print "first" and "second" I get 1 and 4. But
should:

int second = *myset.end();

not return the value of the element *after* the last which is undefined?
I thought that I had to decrement myset.end() by one to get the value 4.
Yes, you have to do it. The code you've posted just invokes undefined
behaviour,
which means that it can result to anything the implementor decides,
particularl y, it can return 4, but on a different platform, it might
crash the
program or return -45246961 or whatever else.

2)

Another thing. As I understand each container supports a specific
iterator. Eg. list only supports forward iterators while set supports
bidirectiona l iterators.

But where (have tried) google do I find a list of each iterator that a
container supports?
Each container has a specific iterator class. This class satisfies
some
concept (similar to interface), theses are the Input/Forward/
Bidirection al/...
[btw. there is a hierarchy between these]. What each container's
iterator has
to satisfy, can be read in the SGI STL manual. For example, with list,
there is

list is a model of reversible container

and on a page about reversible container, there is

.... the iterators must be Bidirectional Iterators

so we know a list must hav an iterator at least as strong as
Bidirectional .

Actually the C++ Standard says that i should be at least be forward see
page 469 item [5]:

http://www.usatlas.bnl.gov/~dladams/...14882-2003.pdf

Yes, but in 23.2.2 item [2], on page 481, there is

A list satisfies all requirements ... of reversible container.

And in 23.1 item 9, there is that reversible containers have
Bidirectional or
Random Access category iterators. Because Random Access are stronger
than Bidirectional and Bidirectional are stronger than Forward (24.1),
we know that
a list have at leas as strong iterators as Bidirectional.
Ok I guess what I was referring to was for Sequences which is the
"general abstraction" and not list which is a "concrete" realization.
Jun 7 '07 #10

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

Similar topics

45
2981
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
2
2396
by: Dave | last post by:
I'm crossposting this to both comp.lang.c++ and gnu.gcc because I'm not sure if this is correct behavior or not, and I'm using the gcc STL and compiler. When calling vector<int>::push_back(0), an iterator that I've set in a loop gets changed. Here's an example of the problem (sorry about the lack of indentation, posting this from Google): ...
18
2276
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
6
1738
by: Adam Hartshorne | last post by:
Hi All, I have the following setup. Two 'std::vector's which i iterate through in a for (iterate through vector1 of types X) { for (iterate through vector2 of types Y) { f(x) }
8
16585
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
8
1983
by: babak | last post by:
Hi everyone I have a problem with Iterators and containers in STL that hopefully someone can help me with. This is what I try to do: I have an associative (map) container and I have a function where I with the help of iterators want to search through the container and remove a certain object in the container. Here is part of the code in...
2
2335
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
8
1943
by: Christian Bruckhoff | last post by:
Hi. I got a problem with deleting items of a vector. I did it like this: void THIS::bashDelPerson() { cout << "Bitte Suchstring eingeben: "; char search; cin >search; vector<Person>::iterator iter; iter = persons.begin();
15
2159
by: fungus | last post by:
I'm moving some code from VC++ 6 to VC++ 2005 and I've run into a nasty problem because iterators are no longer pointers. In the program I'm moving, there's a std::vector of items hidden inside a class and users of the class get to iterate it via a const_iterator. eg.
0
7496
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...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7941
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...
1
7452
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5354
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...
0
5071
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...
0
3485
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
738
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...

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.