Vector iterator problem

N

Nephi Immortal

I guess that Microsoft’s vector code is not good because iterator is
less flexible. If you want to use operator<= or operator>, then
assert is triggered.
I suppose to use operator> if I don’t want to use reverse_iterator.
Why do vector documentation tell to use only operator!= or operator<?
I wish Microsoft should rewrite their vector code. All iterators
must always use signed integer instead of unsigned integer.


#include <vector>
using namespace std;

int main()
{
vector< int > a;
a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
a.push_back( 4 );

vector< int >::iterator B = a.begin();
vector< int >::iterator E = a.end();
vector< int >::iterator I;

int _v;

// OK
for( I = B; I != E; ++I )
_v = *I;

// OK
for( I = B; I < E; ++I )
_v = *I;

// ERROR
for( I = B; I <= E; ++I )
_v = *I;
/* operator<= should call operator< automatically */


// ERROR
for( I = E; I != B; --I )
_v = *I;

// ERROR
for( I = E; I > B; --I )
_v = *I;




return 0;
}
 
V

Victor Bazarov

I guess that Microsoft’s vector code is not good because iterator is
less flexible. If you want to use operator<= or operator>, then
assert is triggered.
I suppose to use operator> if I don’t want to use reverse_iterator.
Why do vector documentation tell to use only operator!= or operator<?
I wish Microsoft should rewrite their vector code. All iterators
must always use signed integer instead of unsigned integer.

I don't understand what you're griping about. I just took your code,
and ran it through the Visual C++ 2010 compiler with NO ERRORS.

Could it be you're using an older version? Get the new one already.
#include<vector>
using namespace std;

int main()
{
vector< int> a;
a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
a.push_back( 4 );

vector< int>::iterator B = a.begin();
vector< int>::iterator E = a.end();
vector< int>::iterator I;

int _v;

// OK
for( I = B; I != E; ++I )
_v = *I;

// OK
for( I = B; I< E; ++I )
_v = *I;

// ERROR
for( I = B; I<= E; ++I )
_v = *I;
^^^^^^^^
This code has undefined behaviour when I==E.
 
L

LR

Nephi said:
I guess that Microsoft’s vector code is not good because iterator is
less flexible. If you want to use operator<= or operator>, then
assert is triggered.

If you try to access an element that isn't in the container, yes.
I suppose to use operator> if I don’t want to use reverse_iterator.
Why do vector documentation tell to use only operator!= or operator<?

Because end() doesn't point to an element in the container. Please see
below.
I wish Microsoft should rewrite their vector code.

MS seems to conform to the standard in this regard.

All iterators
must always use signed integer instead of unsigned integer.

I'm sorry, I'm not sure what you mean by this.
#include <vector>
using namespace std;

int main()
{
vector< int > a;
a.push_back( 1 );
a.push_back( 2 );
a.push_back( 3 );
a.push_back( 4 );

vector< int >::iterator B = a.begin();
vector< int >::iterator E = a.end();
vector< int >::iterator I;

int _v;

// OK
for( I = B; I != E; ++I )
_v = *I;

// OK
for( I = B; I < E; ++I )
_v = *I;

// ERROR
for( I = B; I <= E; ++I )
_v = *I;

As Victor Bazarov pointed out else thread, "This code has undefined
behaviour when I==E."

Since vector<>::end returns an iterator that points to the next element
after the last element in the vector.

The same is true of your loops below.
/* operator<= should call operator< automatically */

Why? It may be that someone would want operator<= instead of operator<.
For example,
E--;
for(I=B; I<=E; ++I)
_v = *I;
 
I

Immortal Nephi

If you try to access an element that isn't in the container, yes.


Because end() doesn't point to an element in the container.  Please see
below.


MS seems to conform to the standard in this regard.


I'm sorry, I'm not sure what you mean by this.














As Victor Bazarov pointed out else thread, "This code has undefined
behaviour when I==E."

Since vector<>::end returns an iterator that points to the next element
after the last element in the vector.

The same is true of your loops below.


Why?  It may be that someone would want operator<= instead of operator<.
For example,
        E--;
        for(I=B; I<=E; ++I)
            _v = *I;








- Show quoted text -- Hide quoted text -

- Show quoted text -

Well, I design my own iterator class to support all 1D, 2D, and 3D in
ONE array.

It looks like this…

iterator_1D< int > iter_1D;
iterator_2D< int > iter_2D;
iterator_3D< int > iter_3D;

/* I create three separate iterator classes */

instead of

typedef std::iterator< std::iterator< std::iterator< int > > >
iter_3D;

I can design nested loops like this.

int size = 64;
int data[ size ] = { …..}; // include plane, row, & column like cube

iter_3D begin( data, 0 );
iter_3D end( data, data + size – 1 ); // omit one extra element in end
iter_3D current;

current = begin; // current is only one variable

while( current.plane() <= end.plane() )
{
/* do something */

while( current.row() <= end.row() )
{
/* do something */

while( current.column() <= end.column() )
{
/* do something */
++current.column();
}

++current.row();
}

++current.plane();
}

I can do single loop to iterator plane, row and column in one array.

while( current <= end )
{
/* do something */
++current;
}

My class design reduces unneeded extra memory space unlike vector with
its own iterator when I reinvent the wheel according to my design
decision.

I am sure you will design your own class yourself.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top