- convert.cpp: code illustrating
conversions to/from strings using <sstream> (that is,
- A common C++ problem is how to append a character to an STL string.
Here's code that does it:
char c = 'y';
string text = "howd", c_as_str(1, c);
text += c_as_str;
assert(text == "howdy");
This uses a constructor for string that takes an integer and a character
and returns a string containing that number of copies of the character.
- cmdline.cpp: A simple program to echo
command-line arguments. This will give you hints about how to process
these arguments. Main is actually invoked with three arguments, but only
the first two are useful for processing the command line. (The third
argument is a pointer to the environment settings; see the man page for
execve for more details.) Note that argv contains the
command itself complete with any path the user used to invoke the command.
To run this example on Unix (aka Linux), type
g++ cmdline.cpp -o cmdline
./cmdline this -is a -list of args.
The response will be
Arg 0: `./cmdline'
Arg 1: `this'
Arg 2: `-is'
Arg 3: `a'
Arg 4: `-list'
Arg 5: `of'
Arg 6: `args.'
Warning: Visual Studio sometimes defaults to creating 'tmain' instead of
'main'. It seems they should be equivalent, but sometimes this causes
problems. Change 'tmain' to 'main' in Visual Studio.
- getline.cpp: sample code reading all
input into a string variable. The old-fashioned way to read input is one
line at a time. The new way is to use the getline() function with
'\0' as the third argument to read all of input in one fell swoop.
The new way is more efficient and certainly simpler, though the amount of
input that can be read is limited by the size of the string variable. If
one may need to handle more than 4 GB of input, one could simply loop until
the end-of-file. To compile and run the program on itself, type:
g++ -o getline getline.cpp
The response will be
Read 382 characters.
- curdate.cpp: getting date and time
in C++ using standard libraries. See man pages about localtime (or help)
for more information.
- Testing C++ Programs - a complete example using
static test methods.
- issue-tracker, Windows/Unix version: code
illustrating connecting to a MySQL database using C++ from both
Windows and Unix/Linux.
- Issue tracking using MySQL: A sample
project for tracking issues in a MySQL database. This illustrates
connecting to a MySQL database from standard C++ programs. This
version connects only from Unix; for a version that connects from either
Windows or Unix see here.
- Socket-based server: this illustrates a
simple way to make a C++-based server on Linux systems.
- simple-sendmail.cpp: a
program which illustrates using the Unix mail command to send mail.
Compile it by
g++ -o simple-sendmail simple-sendmail.cpp
- Lazy Lists and Template Computations in C++: A
pretty bizarre application of templates in C++. Good for fun, probably
not very useful if you're trying to solve a real problem.
- local wxWidgets information