Another blog:

It is easier to write an incorrect program than understand a correct one.

Saturday, April 2, 2011

Python Set C++ Set -- From: Word Aligned

Word Aligned - powered by FeedBurner

Python set is an unordered container designed for fast membership, union, intersection, and differencing operations — much like the mathematical sets I learned about at school — whereas a C++ set is an ordered container, featuring logarithmic access times and persistent iterators.

Think: C++ set ≈ binary tree; Python set ≈ hashed array.

It's apparent which method is correct for which language now. To put something into a binary tree you must recurse down the tree and find where to insert it. Hence std::set::insert() is correct C++. To put something into a hashed array you hash it and add it right there. Hence set.add() is proper Python.

You insert into trees and add to hashes: so if your set is a tree, call S.insert(), and if it's a hash, S.add().

Such logical arguments don't always deliver.

Question: Suppose now that S is a string and you're after its length. Should you use S.length() orS.size()?

Answer: Neither or both.

In Python a string is a standard sequence and as for all other sequences len(S) does the trick. In C++ a string is a standard container and as for all other containers S.size() returns the number of elements; but, being std::stringS.length() does too.

Oh, and the next revision of C++ features an unordered_set (available now asstd::tr1::unordered_set) which is a hashed container. I think unordered_set is a poor name for something which models a set better than std::set does but that's the price it pays for coming late to the party. And you don't std::unordered_set::add elements to it, you std::unordered_set::insert them.


♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥

No comments:

Post a Comment