Vector iterator problem

Discussion in 'C++' started by Nephi Immortal, Aug 4, 2011.

  1. 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;
    }
    Nephi Immortal, Aug 4, 2011
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Aug 4, 2011
    #2
    1. Advertising

  3. Nephi Immortal

    LR Guest

    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;
    > }
    LR, Aug 5, 2011
    #3
  4. On Aug 5, 4:43 am, LR <> 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.
    Immortal Nephi, Aug 11, 2011
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,787
  2. Replies:
    8
    Views:
    1,915
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    561
    James Kanze
    Sep 4, 2007
  4. zl2k
    Replies:
    27
    Views:
    1,578
    Francesco S. Carta
    Sep 7, 2010
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    357
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page