APCS - the apstring class
The apstring class implements character arrays. In many ways apstring objects are easier and safer to use than C-style null-terminated strings. An apstring object is checked at run time for subscript range. Its length is maintained without needing to worry about the null character. Space is automatically allocated when copying an apstring. And there is no need for cryptic code involving pointers.
    Here are examples of the four apstring constructors --

1. 	apstring name;
	// empty (zero-length) apstring
2. 	apstring president ("Washington");
	// initialized to literal string using copy
3. 	apstring firstPres (president);
	// initialized by copy constructor
4. 	name = president;
	// initialized using the assignment operator

Here are some more ways that show how to use apstrings in programs -

#include "apstring.h" // include the header file; make sure the compiler
// knows where it is located
int len = president.length( ); // sets len to 10, the length of "Washington"
int pos = president.find("ash"); // sets pos to 1, the first occurrence of the substring
// if not found, the constant npos, -1, is returned
name = president.substr(4,2); // name gets the value "in"
firstPres = "George " + president; // concatenates two strings, at least one an apstring
if ( president < "Jefferson") //2 strings can be compared, at least one an apstring
    cout << "true";
apstring filename;
cin >> filename; // skips white space, reads one word from input stream
ifstream infile( filename.c_str( ) ); // converts an apstring to a C-style string
while ( getline (infile, president) ) // reads a line of characters from the input stream
    cout << president; // displays the entire line

Continue to:  Unit 2  / Prev  / Next