Connecting Tech Pros Worldwide Help | Site Map

conditional push_back

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 03:56 AM
gelbeiche
Guest
 
Posts: n/a
Default conditional push_back

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, 03:56 AM
Siemel Naran
Guest
 
Posts: n/a
Default 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.