[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    Re: kdebase/kioslave/smtp
From:       Werner Trobin <trobin () kde ! org>
Date:       2003-04-26 18:20:36
[Download RAW message or body]

On Saturday 26 April 2003 19:09, Antonio Larrosa Jiménez wrote:

[...unique and so on...]

> Perhaps with unique, but definitely not with the other methods.
> Just compare the STL help with the Qt one.
> For example, what does vector::erase return ? You know the answer, I know 
> the answer, but does everyone knows it ? I'm sure most people don't.
> 
> Also, the STL has other leaks in other areas different from the 
> documentation too, for example, how do you remove an element from a vector 
> by value ?

By applying the remove_if Algorithm on the whole range (begin/end)? Note that 
remove* doesn't really remove the elements, but pushes them to the end of the 
range, for a good reason. You'll have to apply the remove-erase idiom to get 
rid of those elements.

> Or how do you get an iterator that points to an element in the 
> vector which has a specific value ?

find_if, find_first_of, adjacent_find? There are a lot of search algorithms, 
most labelled find_xyz, xyz_find or search_abc.

The cool thing about the STL is, that the containers are separated from the 
algorithms. This means you only have to implement the algorithm once and it 
works on many containers (yes, I didn't say "all" here on purpose, please 
consult Meyers' excellent Effective STL).

> You have to iterate through all the 
> vector, which is not nice at all (if you know any other way to do it, I'll 
> be glad to hear it :) )

I hope I didn't miss the point, but some variation of

iterator it=find_if(vec.begin(), vec.end(), bind2nd(equal_to<int>(), 42));

should do it for a vector of integers. The iterator would point to the first 
item in the vector with the value 42, or to vec.end() if there isn't such an 
element.

I really recommend Meyers' Effective STL.

Ciao,
Werner
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic