How much of STL is too much?

Posted by Darius Kucinskas on Stack Overflow See other posts from Stack Overflow or by Darius Kucinskas
Published on 2009-04-09T13:26:38Z Indexed on 2010/04/04 8:43 UTC
Read the original article Hit count: 325

Filed under:
|
|
|

I am using a lot of STL code with std::for_each, bind, and so on, but I noticed that sometimes STL usage is not good idea.

For example if you have a std::vector and want to do one action on each item of the vector, your first idea is to use this:

std::for_each(vec.begin(), vec.end(), Foo())

and it is elegant and ok, for a while. But then comes the first set of bug reports and you have to modify code. Now you should add parameter to call Foo(), so now it becomes:

std::for_each(vec.begin(), vec.end(), std::bind2nd(Foo(), X))

but that is only temporary solution. Now the project is maturing and you understand business logic much better and you want to add new modifications to code. It is at this point that you realize that you should use old good:

for(std::vector::iterator it = vec.begin(); it != vec.end(); ++it)

Is this happening only to me? Do you recognise this kind of pattern in your code? Have you experience similar anti-patterns using STL?

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl