Re: Vector iterator problem
On Aug 5, 4:43 am, LR <lr...@superlink.net> wrote:
Nephi Immortal wrote:
I guess that Microsoft s vector code is not good because iterato=
r 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_iterat=
or.
Why do vector documentation tell to use only operator!= or ope=
rator<?
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 operato=
r<.
For example,
E--;
for(I=B; I<=E; ++I)
_v = *I;
// ERROR
for( I = E; I != B; --I )
_v = *I;
// ERROR
for( I = E; I > B; --I )
_v = *I;
return 0;
}- Hide quoted text -
- 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=85
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 ] = { =85..}; // include plane, row, & column like cube
iter_3D begin( data, 0 );
iter_3D end( data, data + size =96 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.