Thursday, October 26, 2006

C++ Tip: Stream Iterators

Write an item n times to an output stream

Solution A: Write a for loop

data_type x = value
for (unsigned int i = 0; i > n; ++i)
  std::cout << x;

Solution B: Use an iterator and an algorithm
std::fill_n(std::ostream_iterator<data_type>(std::cout), n, x);

Solution A has the advantage of simplicity. Almost everyone can remember how to code a for loop. However, Solution B serves as a neat, albeit somewhat trivial, example of the use of a stream iterator working with an algorithm. This is a powerful combination that is rarely explored. So, next time there's something interesting to be done with streams, remember that there is a lot of potential in using the algortihms in the standard library.

I ran some tests to see how the performance of the two solutions compare. With g++ 3.4.2 (mingw-special), there's no difference, regardless of the optimisation options. So, do not hesitate to use the standard library for fear of poor performance.

Wednesday, October 25, 2006

View my (mostly empty) calendar

I've made my calendar available at the bottom of this page. For now, it's mostly empty, but I'm starting to use it more and more. Alternatively, you can go directly to my calendar on Google Calendar.

Tuesday, October 24, 2006


IMG_5968, originally uploaded by smithco.

We spent a day in Aigues-Mortes, a little town held inside a medeval ramparts. It just so happened that we arrived on their Férias (bull festival).

See the whole photo set.

Wednesday, October 18, 2006

Le Pont du Gard

IMG_5808, originally uploaded by smithco.

The Pont du Gard (in the Provence region of France) is one of the largest of the Roman constructions. See it photographed from nearly every angle in a Flickr set

Friday, October 13, 2006

LaTeX Tip: Typesetting Nice Fractions or How to Make xfrac Work

One reason that we all love LaTeX is that it does a beautiful job of typesetting mathematics, provided that you're in an equation environment. Unfortunately, the typesetting of mathematics in inline text does not always work well.

One major problem that can occur is that fractions set inline in text either forces some extra space above and below the line of text or fails to nicely follow the text's current formatting.

The solution is to use the xfrac package, a package that solves all the nasty typesetting issues for fractions. Unfortunately, xfrac doesn't work if it is installed in the default way. A little LaTeX expertise is needed to coerce it into working. The instructions to do so with a MikTeX installation follow (the instructions for other LaTeX installations should be very similar).
  1. Do a full update of using the MikTeX Update Wizard
  2. Install the xfrac package, either automatically by including it in a document or using the MikTeX Package Manager
  3. Get the LaTeX project repository download xbase.tgz, the base package for the experimental features (direct link to the file)
  4. From xbase.tgz, extract the file xbase.ins
  5. Run latex on xbase.ins (the exact command is latex xbase.ins)
  6. Copy the generated style files (template.sty, ldcsetup.sty, xparse.sty, and xparse.sty) to the appropriate places, such that we get:
    • C:\texmf\tex\latex3\template\template.sty
    • C:\texmf\tex\latex3\xparse\ldcsetup.sty
    • C:\texmf\tex\latex3\xparse\xparse.sty
    • C:\texmf\tex\latex3\xtools\xtools.sty
  7. Update the package index (the exact command is texhash)
Now, it is possible to set fractions nicely anywhere in a document with the command \sfrac{numerator}{denominator}. All the details of this command can be found in the xfrac documentation (PDF).

Wednesday, October 04, 2006


I've just about finished overhauling this journal so that it uses the new Blogger Beta engine. So far, it all seems happy and good and untested. If you hit any problems, let me know so I can fix it. And always, I love to get criticisms on the design, so feel free to comment on everything here.