programming languages

Ian McCulloch ianmcc at
Tue Feb 12 11:00:06 EST 2002

On Mon, 11 Feb 2002, Paul Matthews wrote:

> OK I've calmed down now. Does not look like there is
> any real viable alternative to C/C++. OCaml looks
> interesting, by the syntax is a little off putting,
> especially the + +. distinctions. 
> My main whinge with C is the lack of usefull string
> and array abstractions. The same applies to C++. The
> STL seems to add <b>huge</b> amounts of bloat for very
> little real value. Debugging and profiling STL is
> close to impossible due to the bizare an very very
> long names.

What compiler are you using?  In principle, templates don't lead 
inevitably to code bloat but there are some situations where it can 
happen, eg a function is instantiated for N different pointer types, each 
produces exactly the same code, but it is replicated N times...  There 
are tricks to get around this and I imagine that most recent 
compilers/libraries are a lot better in this regard than older ones.

> [ STL aside: How do you binary insert a single value
> into an already sorted array.]

If its a set/multiset/map/multimap then its automatically sorted, so I 
assume your using a std::vector or somesuch.

std::equal_range(Iterator first, Iterator last, Value v) returns a pair of 
iterators (i,j] such that all iterators in (first,i] are less than v, and 
all iterators in (j,last] are greater than v.  All corner-cases taken 
care of.  Most containers have an  insert member function which inserts a 
value immediately before the given iterator.  So, to insert v into the 
container C while leaving it sorted, do

C.insert(std::equal_range(C.begin(), C.end(), v).first, v);

If v already exists in the container, this will insert another at the 
front.  If you want it at the back instead (only useful if the comparison 
defines a partial ordering rather than a total ordering and you care 
about where the inserted elements end up) then use

C.insert(std::equal_range(C.begin(), C.end(), v).second, v);

This should work with at least vector, list and deque.  It probably also 
works with set and multiset as these have a version of insert() that takes 
a 'suggestion' as to where the new element should go.  So, a generic 
version that works on all of the non-associative standard containers is

// function to insert an element into an already sorted container,
// preserving the ordering.
template <class Container, class ValueType>
void insert_sorted(Container& C, const ValueType& v)
   C.insert(std::equal_range(C.begin(), C.end(), v).first, v);

> What I do need now is an embedded language, but one
> with a twist. Because I'm writting relationalish
> database product, it needs to know about NULLs. That
> is:
>   substr(NULL,0,1) is not "" but NULL. 
>   "Hello"+NULL is NULL not "Hello".
>   (a<b) may return NULL not just 'true' or 'false'

I imagine its fairly easy to write a string class in C++ that does this 
sort of thing.


More information about the linux mailing list