Re: for(size_t a=begin();a!=end();++a){}

Discussion in 'C++' started by Chris \( Val \), Jul 13, 2003.

  1. "John Harrison" <> wrote in message
    news:bequq3$84kaj$-berlin.de...
    |
    | "Chris ( Val )" <> wrote in message
    | news:bepeoe$7n5gs$-berlin.de...
    | >
    | > "Agent Mulder" <> wrote in message
    | > news:beok7s$m4a$1.nb.home.nl...
    | > | Hi group,
    | > |
    | > | I am in doubt about the type to
    | > | use in a for-statement when applied
    | > | to a vector. Logic tells me it should be
    | > |
    | > | vector<int>v;
    | > | for(size_t a=v.begin();a!=v.end();++a);
    | > |
    | > | But even Stroustrup uses a plain int
    | > | to traverse the vector (in paragraph
    | > | 5.5 References). What style should I
    | > | adopt?
    | >
    | > Use, 'size_type' ...
    | >
    | > std::vector<int>::size_type Idx( 0 );
    | > for( Idx; Idx < v.size(); ++Idx )
    | > // ...
    | >
    | > Cheers.
    | > Chris Val
    | >
    |
    | vector<T>::size_type is a typedef for the size_type of the allocator that
    | the vector is using. In an STL compatable allocator size_type must be
    | equivalent to size_t. I'm not sure exactly what 'equivalent' means but
    | size_t sure saves a lot of typing.

    Maybe, but even 'Stroustroup' has in the past acknowledged that
    'size_type' is the preferred method. I have since tried to find this
    response on google groups, but have had no luck.

    Cheers.
    Chris Val
    Chris \( Val \), Jul 13, 2003
    #1
    1. Advertising

  2. "Chris ( Val )" <> wrote in message
    news:ber468$85ctr$-berlin.de...
    >
    > "John Harrison" <> wrote in message
    > news:bequq3$84kaj$-berlin.de...
    > |
    > | "Chris ( Val )" <> wrote in message
    > | news:bepeoe$7n5gs$-berlin.de...
    > | >
    > | > "Agent Mulder" <> wrote in message
    > | > news:beok7s$m4a$1.nb.home.nl...
    > | > | Hi group,
    > | > |
    > | > | I am in doubt about the type to
    > | > | use in a for-statement when applied
    > | > | to a vector. Logic tells me it should be
    > | > |
    > | > | vector<int>v;
    > | > | for(size_t a=v.begin();a!=v.end();++a);
    > | > |
    > | > | But even Stroustrup uses a plain int
    > | > | to traverse the vector (in paragraph
    > | > | 5.5 References). What style should I
    > | > | adopt?
    > | >
    > | > Use, 'size_type' ...
    > | >
    > | > std::vector<int>::size_type Idx( 0 );
    > | > for( Idx; Idx < v.size(); ++Idx )
    > | > // ...
    > | >
    > | > Cheers.
    > | > Chris Val
    > | >
    > |
    > | vector<T>::size_type is a typedef for the size_type of the allocator

    that
    > | the vector is using. In an STL compatable allocator size_type must be
    > | equivalent to size_t. I'm not sure exactly what 'equivalent' means but
    > | size_t sure saves a lot of typing.
    >
    > Maybe, but even 'Stroustroup' has in the past acknowledged that
    > 'size_type' is the preferred method. I have since tried to find this
    > response on google groups, but have had no luck.
    >
    > Cheers.
    > Chris Val
    >


    I'm getting my information from Josuttis, who says that the types must be
    equivalent without specifying anywhere what that term means.

    john
    John Harrison, Jul 13, 2003
    #2
    1. Advertising

  3. "Andrey Tarasevich" <> wrote in message
    news:...
    > John Harrison wrote:
    > >> ...
    > >> | vector<T>::size_type is a typedef for the size_type of the allocator

    > > that
    > >> | the vector is using. In an STL compatable allocator size_type must be
    > >> | equivalent to size_t. I'm not sure exactly what 'equivalent' means

    but
    > >> | size_t sure saves a lot of typing.
    > >>
    > >> Maybe, but even 'Stroustroup' has in the past acknowledged that
    > >> 'size_type' is the preferred method. I have since tried to find this
    > >> response on google groups, but have had no luck.
    > >> ...

    > > I'm getting my information from Josuttis, who says that the types must

    be
    > > equivalent without specifying anywhere what that term means.
    > > ...

    >
    > I don't remember seeing this in the standard. The equivalence between
    > 'size_t' and 'std::allocator::size_type' is explicitly declared. But not
    > the equivalence between 'std::allocator::size_type' and
    > 'std::vector::size_type'.
    >
    > Strictly speaking, the original (non-TC1ed) version of the C++ standard,
    > which didn't explicitly require 'std::vector's elements to be stored
    > continuously, didn't really need this equivalence at all. Moreover, it
    > would be flat-out strange to see it declared there.
    >
    > --
    > Best regards,
    > Andrey Tarasevich
    > Brainbench C and C++ Programming MVP
    >


    Yes, you're right I think. I was confused.

    john
    John Harrison, Jul 14, 2003
    #3
    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. Lucas Tam
    Replies:
    0
    Views:
    725
    Lucas Tam
    Apr 13, 2005
  2. Steven T. Hatton
    Replies:
    6
    Views:
    1,008
    Steven T. Hatton
    Aug 22, 2004
  3. Alex Vinokur
    Replies:
    9
    Views:
    770
    James Kanze
    Oct 13, 2008
  4. Alex Vinokur
    Replies:
    1
    Views:
    565
  5. Kurt M. Dresner

    do...end vs. begin..end

    Kurt M. Dresner, Jul 11, 2003, in forum: Ruby
    Replies:
    3
    Views:
    103
Loading...

Share This Page