Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Vector iterator problem

Reply
Thread Tools

Vector iterator problem

 
 
Nephi Immortal
Guest
Posts: n/a
 
      08-04-2011
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;
}
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-04-2011
On 8/4/2011 12:14 PM, Nephi Immortal wrote:
> 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.

> /* 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;
> }



--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
LR
Guest
Posts: n/a
 
      08-05-2011
Nephi Immortal wrote:
> 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;


>
>
> // ERROR
> for( I = E; I != B; --I )
> _v = *I;
>
> // ERROR
> for( I = E; I > B; --I )
> _v = *I;
>
>
>
>
> return 0;
> }


 
Reply With Quote
 
Immortal Nephi
Guest
Posts: n/a
 
      08-11-2011
On Aug 5, 4:43*am, LR <(E-Mail Removed)> wrote:
> Nephi Immortal wrote:
> > * *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;
>
>
>
>
>
> > * *// 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…

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
can I use stl vector iterator to delete a vector of pointers? zl2k C++ 27 09-07-2010 11:47 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments