list::end() decrement

Discussion in 'C++' started by JC, Jul 22, 2012.

  1. JC

    JC Guest

    Just to make sure, is the pre/post decrement operator valid on list::end()?In particular, assuming a non-empty, non-volatile list, will...

    list<x> alist = ...; // non-empty
    list<x>::iterator lastiter = -- alist.end();

    .... always result in lastiter being a valid forward iterator for the last element in alist?

    The reason I'm asking is because the only reference I was able to find was at http://www.sgi.com/tech/stl/BackInsertionSequence.html, which is trustworthy, but I wasn't able to find the validity explicitly stated anywhere (not that it isn't, I just couldn't find it).

    Thanks!
    J
     
    JC, Jul 22, 2012
    #1
    1. Advertising

  2. JC

    Marc Guest

    JC wrote:

    > Just to make sure, is the pre/post decrement operator valid on
    > list::end()? In particular, assuming a non-empty, non-volatile list,
    > will...
    >
    > list<x> alist = ...; // non-empty
    > list<x>::iterator lastiter = -- alist.end();
    >
    > ... always result in lastiter being a valid forward iterator for the
    > last element in alist?


    It may not even compile. On the other hand, the following is fine:

    list<x>::iterator lastiter = alist.end();
    -- lastiter;
     
    Marc, Jul 22, 2012
    #2
    1. Advertising

  3. On Sunday, July 22, 2012 10:24:58 AM UTC+4:30, JC wrote:
    > Just to make sure, is the pre/post decrement operator valid on list::end()? In particular, assuming a non-empty, non-volatile list, will...
    >
    > list&lt;x&gt; alist = ...; // non-empty
    > list&lt;x&gt;::iterator lastiter = -- alist.end();
    >
    > ... always result in lastiter being a valid forward iterator for the lastelement in alist?
    >
    > The reason I'm asking is because the only reference I was able to find was at http://www.sgi.com/tech/stl/BackInsertionSequence.html, which is trustworthy, but I wasn't able to find the validity explicitly stated anywhere (not that it isn't, I just couldn't find it).
    >
    > Thanks!
    > J


    Hi
    I reviewed the C++ final draft international standard (N3290), quickly,
    but I didn't find anything specific about end() iterator decrement.
    BTW, I compiled and ran the following program under GCC 4.7.0
    in two cases: list is empty and list isn't empty. It was fine:
    #include <list>
    #include <iostream>

    int main()
    {
    using namespace std;
    list<int> a_list = {0, 1, 2};
    list<int>::iterator last_iter = --a_list.end();
    cout << *last_iter << '\n';

    return 0;
    }

    I use the following command:
    $ g++ -pedantic -pedantic-errors -std=c++11 list_end_iter.c++
    $ ./a.out
    2
    If the list is empty, 0 is the output.
    Also, I compiled and ran the following program under Visual Studio 2008:
    #include <list>
    #include <iostream>

    int main()
    {
    using namespace std;
    list<int> a_list;
    a_list.push_back(0); a_list.push_back(1); a_list.push_back(2);
    list<int>::iterator last_iter = --a_list.end();
    cout << *last_iter << '\n';

    return 0;
    }
    It was OK, and the output is 2, but if the list is empty,
    there is run-time exception with the following message:
    list iterator not decrement-able.

    HTH,
    -- Saeed Amrollahi Boyouki
     
    Saeed Amrollahi, Jul 22, 2012
    #3
  4. JC

    JC Guest

    On Sunday, July 22, 2012 7:07:40 AM UTC-4, Saeed Amrollahi wrote:
    > On Sunday, July 22, 2012 10:24:58 AM UTC+4:30, JC wrote:
    > &gt; Just to make sure, is the pre/post decrement operator valid on list::end()? In particular, assuming a non-empty, non-volatile list, will...
    > &gt;
    > &gt; list&amp;lt;x&amp;gt; alist = ...; // non-empty
    > &gt; list&amp;lt;x&amp;gt;::iterator lastiter = -- alist.end();
    > &gt;
    > &gt; ... always result in lastiter being a valid forward iterator for thelast element in alist?
    > &gt;
    > &gt; The reason I&amp;#39;m asking is because the only reference I was able to find was at http://www.sgi.com/tech/stl/BackInsertionSequence.html, which is trustworthy, but I wasn&amp;#39;t able to find the validity explicitly stated anywhere (not that it isn&amp;#39;t, I just couldn&amp;#39;t find it).
    > &gt;
    > &gt; Thanks!
    > &gt; J
    >
    > Hi
    > I reviewed the C++ final draft international standard (N3290), quickly,
    > but I didn't find anything specific about end() iterator decrement.
    > BTW, I compiled and ran the following program under GCC 4.7.0
    > in two cases: list is empty and list isn't empty. It was fine:
    > #include &lt;list&gt;
    > #include &lt;iostream&gt;
    >
    > int main()
    > {
    > using namespace std;
    > list&lt;int&gt; a_list = {0, 1, 2};
    > list&lt;int&gt;::iterator last_iter = --a_list.end();
    > cout &lt;&lt; *last_iter &lt;&lt; '\n';
    >
    > return 0;
    > }
    >
    > I use the following command:
    > $ g++ -pedantic -pedantic-errors -std=c++11 list_end_iter.c++
    > $ ./a.out
    > 2
    > If the list is empty, 0 is the output.
    > Also, I compiled and ran the following program under Visual Studio 2008:
    > #include &lt;list&gt;
    > #include &lt;iostream&gt;
    >
    > int main()
    > {
    > using namespace std;
    > list&lt;int&gt; a_list;
    > a_list.push_back(0); a_list.push_back(1); a_list.push_back(2);
    > list&lt;int&gt;::iterator last_iter = --a_list.end();
    > cout &lt;&lt; *last_iter &lt;&lt; '\n';
    >
    > return 0;
    > }
    > It was OK, and the output is 2, but if the list is empty,
    > there is run-time exception with the following message:
    > list iterator not decrement-able.
    >
    > HTH,
    > -- Saeed Amrollahi Boyouki



    Thanks for digging into it! Yes it is hard to find solid information.

    The original page I linked to at http://www.sgi.com/tech/stl/BackInsertionSequence.html says:

    "a.back() | Equivalent to *(--a.end())."

    But I wasn't able to determine if the note was a conceptual or formal example.

    It is working in my code for now, at least, not that that means anything...

    J
     
    JC, Jul 24, 2012
    #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. Hendrix
    Replies:
    1
    Views:
    1,715
    Ivan Vecerina
    Jun 29, 2003
  2. Mark Turney
    Replies:
    11
    Views:
    4,364
    dibeas
    Nov 13, 2006
  3. John
    Replies:
    4
    Views:
    1,179
    Vladimir Marko
    May 17, 2005
  4. Ian Pilcher

    Increment, decrement, overflow, and underflow

    Ian Pilcher, Jan 20, 2005, in forum: C Programming
    Replies:
    5
    Views:
    612
    CBFalconer
    Jan 21, 2005
  5. again some incr/decrement question

    , Jul 2, 2005, in forum: C Programming
    Replies:
    1
    Views:
    302
    Denis Kasak
    Jul 2, 2005
Loading...

Share This Page