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
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;
}
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.
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;
}
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
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.
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();
Re: How can I get the pre-last item of a map?
Quote:
Originally Posted by
2kaud
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...