Connecting Tech Pros Worldwide Help | Site Map

conditional push_back

gelbeiche
Guest
 
Posts: n/a
#1: Jul 23 '05
I have a std::list and I want extend the content of
the list (append a value) only if the value not
already exist.
E.g. I can write for a std::list<int> :

A)
if ( std::find(l.begin(),l.end(), 3) == l.end() )
l.insert(l.end(),3);

or

B)
if ( std::find(l.begin(),l.end(), 3) == l.end() )
l.push_back(3);

I have two questions:
1) Is there a better respectively shorter way to
express this ?
2) Which of both inserts (A or B) is the better one ?
Siemel Naran
Guest
 
Posts: n/a
#2: Jul 23 '05

re: conditional push_back


"gelbeiche" <borstel@fliegenpilz.de> wrote in message
news:m31x8nzkny.fsf@redrat.quark.de...
[color=blue]
> I have a std::list and I want extend the content of
> the list (append a value) only if the value not
> already exist.
> E.g. I can write for a std::list<int> :
>
> A)
> if ( std::find(l.begin(),l.end(), 3) == l.end() )
> l.insert(l.end(),3);
>
> or
>
> B)
> if ( std::find(l.begin(),l.end(), 3) == l.end() )
> l.push_back(3);
>
> I have two questions:
> 1) Is there a better respectively shorter way to
> express this ?
> 2) Which of both inserts (A or B) is the better one ?[/color]

1) Would a std::set<int> be what you want?
2) They're probably the same.


Closed Thread