STL list/map - rest size

Discussion in 'C++' started by Thomas Kowalski, Aug 17, 2006.

  1. Hi everyone,
    I would like if there is an easy way to get the number of elements left
    after an given iterator?
    I have an interator called it somethere between beginning and end of an
    list / map and want to know how often I have to call ++it before I
    arrive at the end of the list. Means how many elements are left after
    the given iterator it. My first solution (see below) is quite
    inefficent.

    int restsize()
    {
    int i=0;
    T::iterator it2 = it;
    for ( ;it2 != somelistormap.end(); ++it2)
    {
    ++i;
    }
    return i;
    }
    Thomas Kowalski, Aug 17, 2006
    #1
    1. Advertising

  2. Thomas Kowalski wrote:
    > I would like if there is an easy way to get the number of elements
    > left after an given iterator?


    Why? What are you trying to accomplish by that?

    > I have an interator called it somethere between beginning and end of
    > an list / map and want to know how often I have to call ++it before I
    > arrive at the end of the list. Means how many elements are left after
    > the given iterator it. My first solution (see below) is quite
    > inefficent.
    >
    > int restsize()
    > {
    > int i=0;
    > T::iterator it2 = it;
    > for ( ;it2 != somelistormap.end(); ++it2)
    > {
    > ++i;
    > }
    > return i;
    > }


    You can take the last element and then see 'std::distance', but it will
    most likely do the same increment, since std::map iterators are not of
    the random-access kind.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 17, 2006
    #2
    1. Advertising

  3. Hi Victor,

    > Why? What are you trying to accomplish by that?


    Need it to set the size of the return datastructure X. X is specified
    stupidly.

    > You can take the last element and then see 'std::distance', but it will
    > most likely do the same increment, since std::map iterators are not of
    > the random-access kind.


    At least its a cleaner :) Thanks.

    Regards,
    Thomas Kowalski
    Thomas Kowalski, Aug 17, 2006
    #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. Marcus
    Replies:
    2
    Views:
    579
    Marcus
    Dec 9, 2005
  2. Replies:
    2
    Views:
    542
    klaus hoffmann
    Feb 22, 2006
  3. Replies:
    5
    Views:
    496
    Markus Schoder
    Apr 16, 2006
  4. kl
    Replies:
    7
    Views:
    1,275
    James Kanze
    Jan 1, 2008
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    340
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page