Iterator question

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...
    , Apr 13, 2007
    #1
    1. Advertising

  2. On 2007-04-13 19:29, 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.
    >
    > Thank you for taking the time to read this.


    If you have a way to get an iterator to the element in question (3 in
    your example) then you can always do something like this (though it
    might not be the most elegant way it'll work):

    std::set<int>::iterator it = getIterator(set, 3); // Get the iterator
    for (; it != set.end(); ++it)
    /*... */

    std::set<int>::iterator it2;
    for (it2 = set.begin(); it2 != it; ++it2)
    /* same thing as above */

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Apr 13, 2007
    #2
    1. Advertising

  3. Erik Wikström wrote:
    > On 2007-04-13 19:29, 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.
    >>
    >> Thank you for taking the time to read this.

    >
    > If you have a way to get an iterator to the element in question (3 in
    > your example) then you can always do something like this (though it
    > might not be the most elegant way it'll work):
    >
    > std::set<int>::iterator it = getIterator(set, 3); // Get the iterator
    > for (; it != set.end(); ++it)
    > /*... */
    >
    > std::set<int>::iterator it2;
    > for (it2 = set.begin(); it2 != it; ++it2)


    OOPS... 'it' == 'end()' here... You likely wanted to preserve its value...

    > /* same thing as above */


    Better

    std::set<...>::iterator it = myset.find ..., e = myset.end();
    for (size_t i = 0, s = myset.size(); i < s; ++i)
    {
    /* do something with 'it' */
    if (++it == e)
    it = myset.begin(); // loop around
    }

    It's a bit less efficient that two loops, but at least it's all in one
    place.

    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, Apr 13, 2007
    #3
  4. Guest

    Thanks guys - this is a great help.
    , Apr 18, 2007
    #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. Hendrik Maryns
    Replies:
    18
    Views:
    1,422
  2. greg
    Replies:
    6
    Views:
    457
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    643
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,157
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,046
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page