Previous | Contents | Index |
The Stopwatch package provides ways to measure intervals of program execution time. The package consists of a single class, Stopwatch. Typically, you use this class during the performance-tuning phase of program development.
Provides the means to measure intervals of time between specified program events.
#include <stopwatch.hxx>Alternative Header
#include <Stopwatch.h>
class Stopwatch { public: Stopwatch(); void start(); void stop(); void reset(); int status() const; double system() const; double user() const; double real() const; static double resolution(); };
Objects of this class measure program execution time and return the result in floating-point seconds. The class includes the start, stop, and reset functions familiar to users of a mechanical stopwatch.You can time the entire program or select certain portions of the program to time; for example, a specified loop or program module. You can create a different Stopwatch object for each independent program activity, and name each according to the activity you intend to measure.
Stopwatch()
Constructs a Stopwatch object with both time and running status initialized to 0.
double real() const
Returns real time (clock time) in double-precision, floating-point seconds. You can call this function while the stopwatch is running.void reset()
Resets the current time measurement to 0 without affecting the value of status(). If status() is initially nonzero, time measurement continues uninterrupted after resetting.double resolution()
Returns the (system dependent) resolution of measured time in double-precision, floating-point seconds.void start()
Begins measuring program execution time when status() is initially 0 (status() becomes nonzero as a consequence of the call). If status()is initially nonzero, the call has no effect.int status() const
Indicates whether the stopwatch is running (returns a value of 1) or not running (returns a value of 0).void stop()
Halts measurement of program execution time when status() is initially nonzero (status() becomes 0 as a consequence of the call). If status()is initially 0, the call has no effect.double system() const
Returns the system CPU time in double-precision, floating-point seconds. You can call this function while the stopwatch is running.double user() const
Returns the user CPU time in double-precision, floating-point seconds. You can call this function while the stopwatch is running.
A typical value for the resolution of execution time is 1/60 second.
Stopwatch w ; w.start() ; //... // some computation you want to time goes here //... w.stop() ; cout << "elapsed time was " << w.user() << "\n"; |
Displays the number of seconds the computation takes to run. The result is a double-precision value.
The String package consists of the single class String. This class provides ways to assign, concatenate, and compare character strings. This class also provides methods for substring creation and for vector access to a character string.
Provides the capabilities for manipulating sequences of characters.
#include <string.hxx>Alternative Header
#include <String.h>
class String { friend ostream &operator<<(ostream &, const String &); friend istream &operator>>(istream &, String &); friend int operator==(const String &, const String &); friend int operator==(const String &, const char *); friend int operator==(const char *, const String &); friend int operator!=(const String &, const String &); friend int operator!=(const String &, const char *); friend int operator!=(const char *, const String &); friend int operator<(const String &, const String &); friend int operator<(const String &, const char *); friend int operator<(const char *, const String &); friend int operator>(const String &, const String &); friend int operator>(const String &, const char *); friend int operator>(const char *, const String &); friend int operator<=(const String &, const String &); friend int operator<=(const String &, const char *); friend int operator<=(const char *, const String &); friend int operator>=(const String &, const String &); friend int operator>=(const String &, const char *); friend int operator>=(const char *, const String &); friend String operator+(const String &, const String &); friend String operator+(const String &, const char *); friend String operator+(const char *, const String &); public: String(); String(const String &); String(const char *); String(const char &); ~String(); String &operator=(const String &); String &operator=(const char *); operator char * () const; operator const char * () const; String &operator+=(const String &); String &operator+=(const char *); String operator()(int, int) const; unsigned int length() const; String upper() const; String lower() const; int match(const String &) const; int index(const String &) const; char operator[](int) const; char &operator[](int); };
This class provides the means for manipulating sequences of characters, each of which is of the type char. For some applications, the services provided are like those provided by the traditional C string library (strcpy, strcmp, and so forth), but are more efficient and convenient in the context of C++. Overloaded operators provide ways to assign, concatenate, and compare strings. New operators provide simple notations for substring creation and vector access into the string.All comparisons are lexicographic, with the ordering dependent on the character set in which the string is encoded.
An index value of 0 indicates the first character in a string object.
String()
Constructs a String object initialized to an empty string.String(const char *s)
Constructs a String object and initializes it to the null-terminated sequence of characters.String(const char &c)
Constructs a String object with a reference to a char datum to initialize the string.String(const String &x)
Constructs a String object with a reference to another String to initialize the first String.~String()
Deletes a String object; no user action is required.
String operator + (const char *s, const String &x)
Concatenates a null-terminated sequence of characters to a String object.String operator + (const String &x, const char *s)
Concatenates a String object with a null-terminated sequence of characters.String operator + (const String &x, const String &y)
Concatenates a String object with another String object.String &operator = (const char *s)
Assigns a String object to a null-terminated sequence of characters.String &operator = (const String &x)
Assigns a String object to another String object.int operator < (const char *s, const String &x)
Tests if a null-terminated sequence of characters is less than a String object; if so, it returns 1. Otherwise, it returns 0.int operator < (const String &x, const char *s)
Tests if a String object is less than a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator < (const String &x, const String &y)
Compares two String objects to determine if the first is less than the second; if so, it returns 1. Otherwise, it returns 0.int operator > (const char *s, const String &x)
Tests if a null-terminated sequence of characters is greater than a String object; if so, it returns 1. Otherwise, it returns 0.int operator > (const String &x, const char *s)
Tests if a String object is greater than a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator > (const String &x, const String &y)
Compares two String objects to determine if the first is greater than the second; if so, it returns 1. Otherwise, it returns 0.String &operator += (const char *st2)
Concatenates a null-terminated sequence of characters to a String object.String &operator += (const String &st2)
Concatenates a String object to another String object.ostream &operator << (ostream &s, const String &x)
Inserts the sequence of characters represented by x into the stream s.istream &operator >> (istream &s, String &x)
Extracts characters from s using the istream extraction operator, then stores characters in x, replacing the current contents of x and dynamically allocating x as necessary.int operator :=,= (const char *s, const String &x)
Tests if a null-terminated sequence of characters is equal to a String object; if so, it returns 1. Otherwise, it returns 0.int operator :=,= (const String &x, const char *s)
Tests if a String object is equal to a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator :=,= (const String &x, const String &y)
Compares two String objects to determine equality. If one is equal to the other, it returns 1; otherwise, it returns 0.int operator != (const char *s, const String &x)
Tests if a null-terminated sequence of characters is not equal to a String object; if so, it returns 1. Otherwise, it returns 0.int operator != (const String &x, const char *s)
Tests if a String object is not equal to a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator != (const String &x, const String &y)
Compares two String objects to determine inequality. If they are not equal, the function returns 1; otherwise, it returns 0.int operator <= (const char *s, const String &x)
Tests if a null-terminated sequence of characters is less than or equal to a String object; if so, it returns 1. Otherwise, it returns 0.int operator <= (const String &x, const char *s)
Tests if a String object is less than or equal to a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator <= (const String &x, const String &y)
Compares two String objects to determine if the first is less than or equal to the second; if so, it returns 1. Otherwise, it returns 0.int operator >= (const char *s, const String &x)
Tests if a null-terminated sequence of characters is equal to or greater than a String object; if so, it returns 1. Otherwise, it returns 0.int operator >= (const String &x, const char *s)
Tests if a String object is equal to or greater than a null-terminated sequence of characters; if so, it returns 1. Otherwise, it returns 0.int operator >= (const String &x, const String &y)
Compares two String objects to determine if the first is equal to or greater than the second; if so, it returns 1. Otherwise, it returns 0.String operator () (int index, int count) const
Creates a new String object defined as a substring of the current String, with index as the starting character and count as the length of the substring.char operator [] (int position) const
Returns the character at the requested position within the string. If the position is past the end of the string, it returns 0. If the position is negative, the results are undefined.char &operator [] (int position)
Returns a reference to the character at the requested position within the string. This reference is potentially invalid after any subsequent call to a non-const member function for the object. If the position is past the end of the string or if the position is negative, the results are undefined.
int index(const String &x) const
Returns the index value of the first position where an element of a String object coincides with the value of x.unsigned int length() const
Returns the length (number of characters) in a String object.String lower() const
Returns a new String object constructed from a String except that every character is lowercase regardless of its original case.int match(const String &x) const
Compares two strings and returns the first index position at which they differ; it returns --1 if the strings match completely. The String argument can be a character pointer.String upper() const
Returns a new String constructed from a String except that every character is uppercase regardless of its original case.
#1 |
---|
String x ("The Times of John Doe"); char *y = "Pink Triangles"; if (x != y) cout << "We have two different strings.\n"; x = y; cout << x; |
The first line of this example provides a character string to the constructor for initialization. The overloaded operators (!=, <<, and =) accept either two String objects or a String and a null-terminate sequence of characters. The last line prints out the following character string:
Pink Triangles
#2 |
---|
String x ("The Times of John Doe"); String a (x(18,3)); // Substring is "Doe" String b (x); // b contains all of x |
In this example, the creation of objecta provides a substring of object x to the constructor for object a. The substring begins at position 18 and has a length of 3 characters. The next line creates the object b and initializes it to contain the same value as x.
#3 |
---|
String x ("World"); String y; y = "Hello"; y += ", " + x + ".\n"; cout << y; |
This example shows string concatenation. The last line prints out the following message:
Hello, World.
Previous | Next | Contents | Index |