Iterator question (ignore other one)

Discussion in 'C++' started by Alex__655321@hotmail.com, Apr 13, 2007.

  1. Guest

    Hello All

    I hope I'm correct posting an STL question here - if not feel free to
    direct me to somewhere more appropriate.

    I'm writing some code using an std::set which I believe is the best
    container to use for this particular problem.

    However I have a case where I need to iterate through the set at an
    arbitrary starting point and traverse all other elements in it.

    For example - if I have a list of 5 elements and wished to start at
    element 3, I would need my iterator to go

    3,
    4.
    5,
    1,
    2

    I am wondering if there is a standard mechanism for doing this or
    maybe another container type that may be appropriate or am I better
    off using a bidirectional iterator and handling the traversal myself?
    I'm asking from an efficiency perspective.

    Thank you for taking the time to read this.

    Alex...
    P.S. Sorry about the previous post.
     
    , Apr 13, 2007
    #1
    1. Advertising

  2. Mark P Guest

    wrote:
    > Hello All
    >
    > I hope I'm correct posting an STL question here - if not feel free to
    > direct me to somewhere more appropriate.
    >
    > I'm writing some code using an std::set which I believe is the best
    > container to use for this particular problem.
    >
    > However I have a case where I need to iterate through the set at an
    > arbitrary starting point and traverse all other elements in it.
    >
    > For example - if I have a list of 5 elements and wished to start at
    > element 3, I would need my iterator to go
    >
    > 3,
    > 4.
    > 5,
    > 1,
    > 2
    >
    > I am wondering if there is a standard mechanism for doing this or
    > maybe another container type that may be appropriate or am I better
    > off using a bidirectional iterator and handling the traversal myself?
    > I'm asking from an efficiency perspective.


    There's no standard mechanism per se, but it's easy enough to do by hand.

    iterator start_it = my_set.find( start_value);
    for( iterator it = start_it; it != my_set.end(); ++it)
    do something;
    for( iterator it = my_set.begin(); it != start_it; ++it)
    do something;

    Mark
     
    Mark P, Apr 13, 2007
    #2
    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. Hendrik Maryns
    Replies:
    18
    Views:
    1,429
  2. greg
    Replies:
    6
    Views:
    460
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    653
    Jim Langston
    Oct 30, 2005
  4. bob
    Replies:
    2
    Views:
    275
    Jack Klein
    Feb 12, 2006
  5. Rob Meade
    Replies:
    6
    Views:
    270
    Rob Meade
    Mar 1, 2004
Loading...

Share This Page