This article presents my first attempt at a C++ string class.
I learnt how program in C with an experience programming in Basic, and so I was both impressed and appalled by C's string handling. Impressed by its efficiency, but appalled by the amount of work required on the programmer's behalf to keep track of memory. Then I learnt Java and Lisp and was impressed how the burden of string memory management had been lifted from the programmer.
When I read C++ creator Bjarne Stroustrup's account of a string class in page 251 of his book The C++ Programming Language, second Edition, I was excited with the possibility that strings could be both easy to use and efficient in implementation. My String class is based on his example, with one extension: the ability to efficiently append text onto an existing string.
Consider the following Java program as an example of the ease of string manipulation:
class Test { int a = {1,2,3,4,5,6,7,8,9,10}; String compose_string() { String s = ""; for (int i=0; i<a.length; i++) { s = s + a[i]; } return s; } public static void main(String args) { System.out.println(compose_string()); } }
To show how my own string class can be used, here is the same example in C++ using my class:
#include "output.hh" int A_SIZE = 10; int a[A_SIZE] = {1,2,3,4,5,6,7,8,9,10}; String compose_string() { String s = ""; for (int i=0; i<A_SIZE; i++) { s << a[i]; } return s; } int main(int argc, char** argv) { cout << compose_string() << endl; } |
I have avoided using C++'s I/O (Input/Output) classes, as part of my investigations was into the design of I/O classes in general. Instead I have written my own ones that are based on the C++ ones. My classes are built upon the relevant C classes.
|
|
||||
|
gccprefs.hh | ||||
|
t-output.cc | ||||
|
string1.tar.gz |
Back to Research Projects |
This page has the following hit count:
|