CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2016
    Posts
    7

    How can I get the pre-last item of a map?

    Hello again,

    my code runs each item of a map and I would like to stop before the last item is reached. I would be thrilled if anybody can explain...

    Code:
    typedef map<int, int>   M;
    typedef M::iterator     It;
    
    M m;
    
    m[1] = 1;
    m[2] = 2;
    m[3] = 3;
    
    
    for (It it=m.begin(); it != m.end(); ++it) {
      
      if (it == m.end() -1)   // ?
        cout << "pre-last item found: " << it->second;   // result 2
    }




    sincerly,
    Necip


    a bit more about me on processing8.wordpress.com

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How can I get the pre-last item of a map?

    .end() points beyond the last element so the previous element to .end() is actually the last element. So if you want the pre-last element you need the previous to the previous of the .end(). Consider
    Code:
    #include <map>
    #include <iterator>
    #include <iostream>
    using namespace std;
    
    typedef map<int, int> M;
    
    int main()
    {
    	M m { {1, 1}, {2, 2}, {3, 3} };
    
    	const auto prelast = prev(prev(m.end()));
    
    	for (auto it = m.begin(); it != m.end(); ++it)
    		if (it == prelast)
    			cout << "pre-last item found: " << it->second << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: How can I get the pre-last item of a map?

    It depends on what you are trying to achieve exactly. Anyways, the last item can be found at the index before end, which can be found with either "prev(m.end())" with C++11, or with "--m.end()" (less recommended, as it returns a reference to a temporary).

    From there, you can do something like this:

    Code:
    It beforeLast = prev(m.end());
    for (It it=m.begin(); it != beforeLast; ++it) {
      // do things
    }
    // do thing to beforeLast.
    If you want to explicitly handle it in your loop, then this works:
    Code:
    It beforeLast = prev(m.end());
    for (It it=m.begin(); it != m.end(); ++it) {
      if (it == beforeLast) {
        // special case
      }
    }
    // do thing to beforeLast.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How can I get the pre-last item of a map?

    Also consider which displays all items of the map up to but not including the last element
    Code:
    #include <map>
    #include <iterator>
    #include <iostream>
    using namespace std;
    
    typedef map<int, int> M;
    
    int main()
    {
    	M m { {1, 1}, {2, 2}, {3, 3} };
    
    	const auto last = prev(m.end());
    
    	for (auto it = m.begin(); it != last; ++it)
    			cout << it->second << endl;
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2016
    Posts
    7

    Re: How can I get the pre-last item of a map?

    Thank you friends for the hint with "prev". unfortunately it does not work on C++ Builder XE7!?!
    But I could now find the solution for me seeking "prev" in google :

    boost
    via stackoverflow

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How can I get the pre-last item of a map?

    If the compiler you are using does not support prev, then it is not c++11 conformant. IMO I would suggest you consider changing to a compiler that is at least c++11 compliant and preferably c++14 compliant.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: How can I get the pre-last item of a map?

    As monarch_dodra indicated in post #3, prev() can be replaced by pre-decrement.

    Code:
    const auto prelast = prev(prev(m.end()));
    can be replaced by
    Code:
    const auto last = --(--m.end());
    and
    Code:
    const auto last = prev(m.end());
    by
    Code:
    const auto last = --m.end();
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Jan 2016
    Posts
    7

    Re: How can I get the pre-last item of a map?

    Quote Originally Posted by 2kaud View Post
    If the compiler you are using does not support prev, then it is not c++11 conformant. IMO I would suggest you consider changing to a compiler that is at least c++11 compliant and preferably c++14 compliant.
    I figured out that the C++ Builder XE7 IS c++11 conformant, but NOT in the 32-bit Version which our application is compiled with.
    Belief me I would love to work with the last releases of C++ but unfortunatelly I am restricted by enterprise policies...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured