Thank you all very much for help !
To sum up: according to what has been said this version is correct:
Code:
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef map<int,int>::iterator iter;
iter find(map<int,int> &s, int x) {
iter it=s.lower_bound(x);
return it==s.begin() ? s.end() : --it;
}
int main() {
map<int,int> t;
int x=0;
for(int i=4;i<=8;i+=2) t[i]=++x;
for(int i=2;i<=9;++i) {
iter it=find(t,i);
printf("find(%d):", i);
if(it==t.end()) printf(" NONE\n");
else printf("(%d,%d)\n",it->first,it->second);
}
return 0;
}
Why ? If map is empty, then lower_bound(x)==s.end()==s.begin() and result is s.end(), and if map is not empty and is the lower_bound(x) is s.begin(), then result is s.end(), and if the lower_bound(x)!=s.begin() then --lower_bound(x) is the answer (even if lower_bound(x) is s.end(), since performing --s.end() is correct in this case).