Python and STL efficiency

Mc Osten riko at despammed.com
Tue Aug 22 19:31:01 EDT 2006


Tim N. van der Leeuw <tim.leeuwvander at nl.unisys.com> wrote:

> NB: Your code now tests for address-equality. Does it also still test
> for string-equality? It looks to me that it does, but it's not quite
> clear to me.

It does it.

    set<string*> b(a.begin(), a.end());
    set<string> c; // well ordered set (b is ordered by address)
    for (set<string*>::iterator it=b.begin(); it!=b.end(); it++)
        c.insert(**it);
    copy(c.begin(), c.end(), ostream_iterator<string>(cout, "\n"));

When we populate the first set, we get rid of all strings with same
object id/address (it test equality of pointers). Then we populate
another set (and the default equality test is on strings).

However, I would have written the code using a proper compare function
rather than using two sets. In this particular case the number of
elements of the first set is negligible in respect of the initial vector
size, thus copying it again does not take a lot of time.
But such code is optimized for the problem itself: in the real world I
suppose we would have passed set a proper comparison function that
checks address and then string equality.


-- 
blog:  http://www.akropolix.net/rik0/blogs | Uccidete i filosofi,
site:  http://www.akropolix.net/rik0/      | tenetevi riso e
forum: http://www.akropolix.net/forum/     | bacchette per voi.



More information about the Python-list mailing list