Connecting Tech Pros Worldwide Help | Site Map

conditional push_back

  #1  
Old July 23rd, 2005, 04:56 AM
gelbeiche
Guest
 
Posts: n/a
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 ?
  #2  
Old July 23rd, 2005, 04:56 AM
Siemel Naran
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional structure vs. function pointers Gary Wessle answers 5 November 9th, 2006 01:55 PM
Meta class programming question Jon Slaughter answers 21 September 12th, 2005 11:05 AM
unexpected result using std::list Mike Pemberton answers 3 July 19th, 2005 08:13 PM
buffer options Allen answers 5 July 19th, 2005 05:16 PM