The Lazy Programmer

January 5, 2009

Returning multiple values from a function in C++

Filed under: C#,Programming — ferruccio @ 10:22 pm
Tags: , ,

Ideally all functions should return just one value. There are many times, however, when returning more than one value makes a function so much more convenient. The classic example of this convenience is file input. When we read data from a file we want to know two things: Did we reach the end of the file? and if not, what is the next piece of data from the file.

Sometimes, we can encode multiple return values into one. For many of us, the first C idiom we learned from K&R is processing input a character at a time:

int ch;
while ((ch = getchar()) != EOF) {
    // do character processing...
}

This works because the EOF macro was set to something outside the range of valid characters (usually -1). While this approach can work fairly well for simple cases, it quickly breaks down as the types we wish to return get more complex.

(more…)

August 3, 2008

Formatting Output with Boost

Filed under: C#,Programming — ferruccio @ 9:20 pm
Tags: , ,

Sometimes a GUI is overkill for a project. You just need a simple tool to do some task. Perhaps it needs to be scripted. So you whip up a console mode program and you eventually have to output something. At this point, many developers will simply ignore the C++ iostreams library and reach for good old printf(). I can certainly understand why. The iostreams objects are easy enough to use for simple formatting tasks. However, when you need to do something more sophisticated, you will often find yourself digging through reference material, muttering "this should be easy…"

(more…)

June 4, 2008

C++: Using Boost ranges to simplify enumerations

Filed under: C#,Programming — ferruccio @ 8:58 pm
Tags: , , ,

Let’s say you’ve created a class which encapsulates an STL-style container and you now need to expose the data in that container. There are a few possibilities which we’ve probably all tried and found to be unsatifactory.

(more…)

April 22, 2008

Using Boost to tokenize strings

Filed under: C#,Programming — ferruccio @ 9:55 pm
Tags: , ,

Like most C++ programmers that came from a C background, whenever I needed to parse a string into a bunch of tokens, I reached for strtok(). Sure, it’s not thread-safe. But I can take care of that later if it becomes an issue, right?

Eventually, I ran across the STL and learned to put my strings and other objects into containers. But strtok would eventually rear it’s ugly head to break the abstraction of STL strings just so I could split up a string without putting in too much effort.

At some point, I started playing with Boost. If you’re a C++ programmer and you don’t know what Boost is, you need to go to boost.org and take a look at it right now because I guarantee that you are doing way too much unnecessary work without it. Go ahead, I’ll wait until you’re done looking around.

(more…)

Create a free website or blog at WordPress.com.