PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > some operations present only for list<T>
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
some operations present only for list<T>

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 08h30   #1
subramanian100in@yahoo.com, India
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut some operations present only for list<T>

There are some operations like sort, remove, remove_if which are
available as list<T> member functions; but vector<T>, deque<T> do not
seem have those functions. If am correct in this, what is the reason
for that. Moreover sort, remove, remove_if are available in
<algorithm> also. Then why does list<T> have these member operations ?

Kindly clarify.

Thanks
V.Subramanian
  Réponse avec citation
Vieux 06/02/2008, 09h37   #2
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some operations present only for list<T>

On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.com> wrote:
> There are some operations like sort, remove, remove_if which are
> available as list<T> member functions; but vector<T>, deque<T> do not
> seem have those functions. If am correct in this, what is the reason
> for that. Moreover sort, remove, remove_if are available in
> <algorithm> also. Then why does list<T> have these member operations ?
>


The algorithms in <algorithm> operate on ranges, not containers.
Therefore, an algorithm like std::remove_if will not actually remove
any elements, just copy the elements not removed to the front of the
range, and return a new end iterator.

The member functions of list naturally operate on the list itself.
Therefore list<T>::remove and list<T>::remove_if will actually erase
the elements from the list.

In general using the member functions where present will be faster,
and they often do slightly different things.

As for std::sort, this algorithm requires random access iterators,
while list only has bidirectional iterators. Therefore it needs its
own sort function.
  Réponse avec citation
Vieux 06/02/2008, 11h23   #3
Phil Endecott
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some operations present only for list<T>

Triple-DES wrote:
> On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
> <subramanian10...@yahoo.com> wrote:
>> There are some operations like sort, remove, remove_if which are
>> available as list<T> member functions; but vector<T>, deque<T> do not
>> seem have those functions. If am correct in this, what is the reason
>> for that. Moreover sort, remove, remove_if are available in
>> <algorithm> also. Then why does list<T> have these member operations ?


> As for std::sort, this algorithm requires random access iterators,
> while list only has bidirectional iterators. Therefore it needs its
> own sort function.


Does anyone know why this is not done with a specialisation of the sort
algorithm?

The same applies to find, i.e.

map m;
vector v;

m.find(...); // O(log N)
find(v.begin(),v.end(),...); // O(N)
find(m.begin(),m.end(),...); // O(N)
// but could be O(log N) if specialised?


Phil.

  Réponse avec citation
Vieux 06/02/2008, 13h44   #4
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some operations present only for list<T>

On 6 Feb, 12:23, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
> Triple-DES wrote:
> > On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
> > <subramanian10...@yahoo.com> wrote:
> >> There are some operations like sort, remove, remove_if which are
> >> available as list<T> member functions; but vector<T>, deque<T> do not
> >> seem have those functions. If am correct in this, what is the reason
> >> for that. Moreover sort, remove, remove_if are available in
> >> <algorithm> also. Then why does list<T> have these member operations ?

> > As for std::sort, this algorithm requires random access iterators,
> > while list only has bidirectional iterators. Therefore it needs its
> > own sort function.

>
> Does anyone know why this is not done with a specialisation of the sort
> algorithm?


It may not be impossible to do it, but I for one can't think of a good
way to implement it. The naive approach

template<typename T>
void sort(typename std::list<T>::iterator first, typename
std::list<T>::iterator last)

doesn't work because the nested type can not be deduced (14.8.2.4/4).
Obtaining the actual container from just the two iterators could also
be a problem.
  Réponse avec citation
Vieux 07/02/2008, 09h57   #5
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: some operations present only for list<T>

On Feb 6, 12:23 pm, Phil Endecott <spam_from_usenet_0...@chezphil.org>
wrote:
> Triple-DES wrote:
> > On 6 Feb, 09:30, "subramanian10...@yahoo.com, India"
> > <subramanian10...@yahoo.com> wrote:
> >> There are some operations like sort, remove, remove_if which are
> >> available as list<T> member functions; but vector<T>, deque<T> do not
> >> seem have those functions. If am correct in this, what is the reason
> >> for that. Moreover sort, remove, remove_if are available in
> >> <algorithm> also. Then why does list<T> have these member operations ?

> > As for std::sort, this algorithm requires random access iterators,
> > while list only has bidirectional iterators. Therefore it needs its
> > own sort function.


> Does anyone know why this is not done with a specialisation of
> the sort algorithm?


Because the algorithms don't take containers, only iterators.
Once you've only got begin and end, you're stuck; you can't get
back to the container. (Note too that the iterators I pass to
std::sort may not cover the entire container.)

> The same applies to find, i.e.


> map m;
> vector v;


> m.find(...); // O(log N)
> find(v.begin(),v.end(),...); // O(N)
> find(m.begin(),m.end(),...); // O(N)
> // but could be O(log N) if specialised?


Same problem.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13947 seconds with 13 queries