Escolar Documentos
Profissional Documentos
Cultura Documentos
Compiling in C++ Data Types Control Structures Operators Input / Output Pointers Functions Arrays String Class
1/17/2008 CS 3424 2
Files that contain definitions are translated by the compiler into an intermediate form called object files. One or more object files are combined with to form the executable file by the linker.
1/17/2008
CS 3424
1/17/2008
CS 3424
incorporate the declarations of the iostream and cstdlib libraries into the source code. If your program is going to use a member of the standard library, the appropriate header file must be included at the beginning of the source code file.
1/17/2008
CS 3424
tells the compiler to make all names in the predefined namespace std available. The C++ standard library is defined within this namespace. Incorporating the statement
using namespace std;
1/17/2008
CS 3424
1/17/2008
CS 3424
Another Way
1 #include <cstdlib> 2 #include <iostream> 3 /* This program inputs two numbers x and y and outputs their sum */ 4 int main ( ) { 5 int x, y; 6 std::cout << Please enter two numbers:; 7 std::cin >> x >> y; 8 int sum = x + y; 9 std::cout << Their sum is << sum << std::endl; 10 return EXIT_SUCCESS; 11 }
1/17/2008 CS 3424 9
where the code for the function appears between the { and the }.
1/17/2008 CS 3424 10
The Preprocessor
The compiler (effectively) makes several passes through the source program. The first of these passes is done by whats known as the preprocessor.
1. 2. 3. 4. 5. 6.
Replace trigraphs with their equivalent Splice long lines into a single line. Remove comments and replace by a single space. Split the input file into tokens Carry out preprocessing directives Expand macros
1/17/2008
CS 3424
11
1/17/2008
CS 3424
12
Comments
There are two types of comments: Form 1:
A comment begins with the characters /* and ends with the characters */
Macros
Macros are defined by the forms:
#define macro-name macro-definition #define macro-name(parameters) macro-definition
Form 2:
A comment begins with the characters // and ends at the end of the current line
All characters of a comment are replaced by a single space by the preprocessor. Note that a /* that follows a // is ignored. A // that appears after a /* is also ignored. A comment that begins with a /* is terminated by the first */ that is encountered.
The definition ends at the end of the current line. Macros requiring longer lines use long-line splicing.
Examples:
#define NULL 0 #define MAX(x, y) ((x) > (y) ? (x) : (y))
1/17/2008
CS 3424
13
1/17/2008
CS 3424
14
Macro Expansion
Given the previous examples.
if (ptr == NULL)
Conditional Compilation
Forms:
#ifdef macro-name code to be compiled if macro-name is defined #else code to be compiled if macro-name is not defined #endif
and
c = max(a, b);
or
#ifndef macro-name code to be compiled if macro-name is not defined #else code to be compiled if macro-name is defined #endif
1/17/2008
CS 3424
15
1/17/2008
CS 3424
16
function declaration
#ifdef __cplusplus } #endif
1/17/2008
CS 3424
17
1/17/2008
CS 3424
18
Compound Statements
A sequence of statements between a { and } character is called a compound statement. Syntactically, a compound statement can be used in place of a single statement.
is reserved for standard library headers. #include "file-name" is used for user-defined include files.
The convention is that user-defined include files will end with the extension .h. Note that the standard library headers do not end with .h.
1/17/2008
CS 3424
19
1/17/2008
CS 3424
20
structured
array struct union class
address
float double long double pointer reference
1/17/2008
CS 3424
21
1/17/2008
CS 3424
22
3
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4
@ A B C D E F G H I J K L M N O
5
P Q R S T U V W X Y Z [ \ ] ^ _
6
` a b c d e f g h i j k l m n o
7
p q r s t u v w x y z { | } ~
delete
1/17/2008
CS 3424
23
1/17/2008
CS 3424
24
Numeric Constants
1234 is an int 1234U or 1234u is an unsigned int 1234L or 1234l is a long 1234UL or 1234ul is an unsigned long 1.234 is a double 1.234F or 1.234f is a float 1.234L or 1.234l is a long double.
1/17/2008 CS 3424
Character Constants
The form 'c' is a character constant. The form '\xhh' is a character constant, where hh is a hexadecimal digit, and hh is between 00 and 7F. The form '\x' where \x is one of the following is a character constant.
25
1/17/2008
CS 3424
26
Escape Sequences
The form \x is called an escape sequence, it represents one of the following special characters: \a bell \\ backslash \b backspace \? question mark \f formfeed \' single quote \n newline \" double quote \r carrige return \ooo octal number \t horizontal tab \xhh hexadidecimal number \v vertical tab
String Constants
The form "sequence of characters" where sequence of characters does not include " is called a string constant. Note escape sequences may appear in the sequence of characters. String constants are stored in the computer as arrays of characters followed by a '\0'.
1/17/2008
CS 3424
27
1/17/2008
CS 3424
28
Control Stuctures
Control Structure Purpose
Used to write a decision with condition selecting the alternative to be executed.
Syntax
if ... else
switch
while
if (condition) { . . . } else { . . . } switch (selector) { Used to write a decision with scalar values (integers) case label: statements; break; that select the alternative to case label: statements; be executed. break; . . . default: statements; } while (condition) { Used to write a loop that . . . specifies the repetition condition in the loop header. }
1/17/2008
CS 3424
29
1/17/2008
CS 3424
30
Purpose
Used to write a loop that executes at least once. The repetition condition is at the end of the loop. Used to write a loop that specifies the initialization, repletion condition, and update steps in the loop header.
Syntax
do { . . . } while (condition); for (initialization; condition; update) { . . . }
There are several coding styles. The one used in this text is:
Place a { on the same line as the condition for an if, while, or for statement. Indent each line of the controlled compound statement. Place the closing } on its own line, indented at the same level as the if, while, or for. For else conditions, use the form:
} else {
for
1/17/2008
CS 3424
31
1/17/2008
CS 3424
32
Nested If Statements
If there are multiple alternatives being selected,
the if that appears within an else part should be on the same line as the else. Example:
if (operator == '+') { result = x + y; add_op++; } else if (operator == '-') { result = x y; subtract_op++; }
Expressions
Member selection & indexing
Class_name.member Pointer -> member Array [exp]
Arithmetic operators
+, -, *, /, %
Relational operators
<, >, <=, >=, ==, !=
Logical operators
!, &&, ||
1/17/2008
CS 3424
33
1/17/2008
CS 3424
34
Bitwise Operators
~ (complement) & (bitwise and) ^ (bitwise exclusive or) | (bitwise or) << (shift left) >> (shift right)
Other Operators
:: (scope resolution) ?: (conditional expression) stream << var stream >> exp
Assignment Operators
+=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
1/17/2008 CS 3424 35 1/17/2008 CS 3424 36
Operator Precedence
Rank Operator
1
[] () . -> ++ -++ -* & + ! ~ new * / % + << >>
Operation
Array subscript Function call Member access Member access Postfix increment or decrement Prefix increment or decrement Pointer de-referencing operator Address of operator Unary plus or minus Complement Bitwise complement Object creation Multiply, divide, remainder Addition, Subtraction Shift left Shift right
Rank Operator
6 7 8 9 10 11 12 13 14
< <= > >= == ! & ^ | && || ?: = *= /= &= += -= <<= >>= &= |=
Operation
Associativity
3 4 5
Less than, Less than or equal Greater than, Greater than or equal Equal to Not equal to Bitwise and Exclusive or Bitwise or Logical and Logical or Conditional Assignment Right Compound Assignment
1/17/2008
CS 3424
37
1/17/2008
CS 3424
38
Postfix
x++
1/17/2008
CS 3424
39
1/17/2008
CS 3424
40
Example
If i is an int and y a double
i = int(y);
1/17/2008
CS 3424
41
1/17/2008
CS 3424
42
C-Style Casts
You can also perform explicit conversion using the form:
(new-type) expression
This form is inherited from the C programming language and its use is discouraged in C++ programs. C++ has other type conversion operators (also called cast operators) for conversion among user-defined (i.e. Class) types.
These are discussed later, when they are used.
If the boolean-exression is true, then the result is value1 otherwise it is value2. In most cases the same effect can be achieved using the if statement.
1/17/2008
CS 3424
43
1/17/2008
CS 3424
44
Input / Output
Both Java and C++ use streams Java Input C++
Input/Output Streams
An input stream is a sequence of characters.
They may be from the keyboard, a file, or some other data source (e.g. a network socket).
Scanner scan = new Scanner cin >> x; (System.in); cin >> x >> a; a = scan.nextInt();
Output System.out.println(x);
System.out.print(a);
1/17/2008
CS 3424
45
1/17/2008
CS 3424
46
Standard input is generally from the keyboard, but may be assigned to be from a file. Standard output and standard error are generally to the console, but may be assigned to a file.
Processing
The first non-space character is read. Starting with the first non-space character, characters are read up to the next space. If the first non-space character is a digit (or + or -), characters are read until a non-digit is encountered. The sequence of digits is then converted to an integer value of the specified type. If the first non-space character is a digit (or + or -), characters are read as long as they match the syntax of a floating-point literal. The sequence of characters is then converted to a floating-point value of the specified type.
CS 3424 48
1/17/2008
CS 3424
47
1/17/2008
Behavior
Returns true if there is no more data available from the input stream, and there was an attempt to read past the end. bool fail() const Returns true if the input data did not match the expected format, or if there is an unrecoverable error. bool bad() const Returns true if there is an unrecoverable error. bool operator!() const Returns fail(). This function allows the istream variable to be used directly as a logical variable. operator void*() const Returns a null pointer if fail() is true, otherwise returns a non-null pointer. This function allows the use of an istream variable as a logical variable.
1/17/2008
CS 3424
49
1/17/2008
CS 3424
50
The character is output. The sequence of characters in the string is output. The integer value is converted to decimal and the characters are output. Leading zeros are not output unless the value is zero, in which case a single 0 is output. If the value is negative, the output is preceded by a minus sign. The floating-point value is converted to a decimal representation and output. By default a maximum of six digits is output. If the absolute value is between 10-4 and 106, the output is in fixed format; otherwise it is in scientific format.
If a floating-point value is a whole number, the decimal point is not shown. The decimal point is always shown for floating-point output. Sets the format flag so that on input white space (space, newline, or tab) characters are skipped. Sets the format flag so that in input white space (space, newline, or tab) characters are read. On output, the value is right-justified. On output, the value is left-justified. The input/output is in base 10. The input/output is in base 16. Floating-point output is in fixed format Floating-point output is in scientific format. On input, whitespace is skipped. This is a one-time operation and does not clear the format flag. On output, a newline character is written and the output buffer is flushed.
1/17/2008
CS 3424
51
1/17/2008
CS 3424
52
Behavior
Sets the minimum width of the next output. After this the minimum width is reset to the default value of 0. setprecision(size_t) Sets the precision. Depending on the output format, the precision is either the total number of digits (scientific) or the number of fraction digits (fixed). The default is 6. setfill(char) Sets the fill character. The default is the space. resetiosflags(ios_base::fmtflags) Clears the format flags set in the parameter. setiosflags(ios_base::fmtflags) Sets the format flags set in the parameter.
Format Example
Fixed
123.456789
Description
Scientific
General
Output is of the form ddd.ffffff where the number of digits following the decimal point is specified by the precision. 1.2345678e+002 Output is of the form d.fffffennn where the number of digits following the decimal point is controlled by the value of precision. (On some systems only two digits for the exponent are displayed.) 1.23456e+006 A combination of fixed and scientific. If the absolute 1234567 value is between 10-4 and 106, output is in fixed format; 123.4567 otherwise it is in scientific format. The number of 1.234567e-005 significant digits is controlled by the value of precision.
CS 3424 54
1/17/2008
CS 3424
53
1/17/2008
File Streams
The header <fstream> defines the classes
ifstream ofstream An istream associated with a file An ostream associated with a file
Behavior
Constructs an ifstream that is not associated with a file. Constructs an ifstream that is associated with the named file. By default, the file is opened for input. ofstream() Constructs an ofstream that is not associated with a file. ofstream(const char* file_name, Constructs an ofstream that is ios_base::openmode mode = ios_base::out) associated with the named file. By default, the file is opened for output. void open(const char* file_name, Associated an ifstream or and ios_base::openmode) ofstream with the named file and sets the openmode to the specified value.
1/17/2008
CS 3424
55
1/17/2008
CS 3424
56
Openmode Flags
openmode Meaning
in out binary trunc app
The file is opened for input. The file is opened for output. No translation is made between internal and external character representation. The existing file is discarded and a new file is written. This is the default and applies only to output. Data is appended to the existing file. Applies only to output.
1/17/2008
CS 3424
57
1/17/2008
CS 3424
58
Object Declaration
Form:
type-name name; type-name name = initial-value; type-name name(argument-list);
Object Lifetimes
Objects are created when they are declared. Objects declared within the scope of a function are destroyed when the function is exited. Objects declared in a block (between { and }) are destroyed when the block is exited. Objects declared outside the scope of a function (called global objects)
Are created before main is called Are destroyed after main exits Objects created using the new operator must be destroyed using the delete operator.
Example
int i; string s = "Hello"; double x = 5.5; double y(6.7); point p(x, y);
1/17/2008
CS 3424
59
1/17/2008
CS 3424
60
10
Memory Allocation
It is also possible, during execution of the program, to allocate new memory to the program to store variable values. This is done by using the new and delete operators (not functions)
1/17/2008
CS 3424
61
1/17/2008
CS 3424
62
Allocation of Memory
static and automatic data STATIC ALLOCATION dynamic data DYNAMIC ALLOCATION
Addresses in Memory
when a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is the address of the variable int x; float number; char ch; 2000
x
Dynamic allocation is the allocation of memory space at run time by using operator new.
2002
number
CS 3424
2006
ch
64
1/17/2008
CS 3424
63
1/17/2008
Pointers
A pointer is an object that refers to another object. A pointer object contains the memory address of the object it points to. Example:
double x = 5.1234; double *px = &x;
int x; float number; char ch; cout << Address of x is << &x << endl; cout << Address of number is << &number << endl; cout << Address of ch is << &ch << endl;
1/17/2008
CS 3424
65
1/17/2008
CS 3424
66
11
Basics
Pointer
Object whose value represents the location of another object In C++ there are pointer types for each type of object
Pointers to int objects Pointers to char objects Pointers to Rational objects
Pointer Declaration
Form
type-name* pointer-variable; type-name* pointer-variable = &object; type-name *pointer-variable; type-name *pointer-variable = &object;
T* v versus T *v
Using the form
double* px;
Example:
*px = 1.2345;
Results in
states that the expression *px is of type double, thus px must be a pointer-todouble.
1/17/2008
CS 3424
69
1/17/2008
CS 3424
70
Syntax
Examples of uninitialized pointers
Indicates pointer object
Memory Depiction
int i = 1; char c = 'y'; int *ptr; ptr = &i; char *t; t = &c;
t points to address of a character i 1 c 'y' t ptr ptr points to the address of an integer
1/17/2008
CS 3424
72
12
Address Operator
& use is not limited to definition initialization
int i = 1; int j = 2; int *ptr; // ptr points to location of i ptr = &i; *ptr = 3; // contents of i are updated ptr = &j; // ptr points to location of j *ptr = 4; // contents of j are updated cout << i << " " << j << endl;
73 1/17/2008 CS 3424 74
declares that px is a pointer-to-double, but py is a double. To declare multiple pointer variables in one declaration:
double *px, *py;
1/17/2008
CS 3424
CS 3424
76
Another Example
char ch; ch = A; char* q; q = &ch; *q = Z; char* p; p = q;
1/17/2008
Another Example
4000 4000 A ch 6000
5000 4000 q
6000 4000 p
A ch 5000 4000 q
A
ch2
7000 6000 p
13
Future versions of C++ will have a reserved-word for the null pointer literal.
1/17/2008
CS 3424
Member Indirection
Consider TimeType t(1,15,0); TimeType *tPtr = &t; To select a member of t through indirection using tPtr operator precedence requires we do the following Invokes member Insert of the object to which tPtr points (t) (*tPtr).Increment(); This syntax is clumsy, so C++ provides the indirect member selector operator -> tPtr->Increment(); Invokes member Insert of the
object to which tPtr points (t)
1/17/2008 CS 3424 81 1/17/2008
// illegal
4000
ptr1
CS 3424
ptr2
CS 3424
14
If memory is available, in an area called the heap (or free store) new allocates the requested object or array, and returns a pointer to (address of ) the memory allocated. Otherwise, program terminates with error message. The dynamically allocated object exists until the delete operator destroys it.
1/17/2008 CS 3424 85 1/17/2008
ptr
CS 3424
86
char* ptr;
3000
char* ptr;
3000
ptr
ptr
3000
Operator delete returns to the free store memory which was previously allocated at run-time by operator new. The object or array currently pointed to by the pointer is deallocated, and the pointer is considered unassigned.
Note that pointer-variable must have been initialized by the new operator. Attempts to use delete on some other pointer value will probably cause a runtime error.
90
1/17/2008 CS 3424 89 1/17/2008 CS 3424 90
15
If the value of the pointer is 0 there is no effect. Otherwise, the object or array currently pointed to by Pointer is deallocated, and the value of Pointer is undefined. The memory is returned to the free store. Square brackets are used with delete to deallocate a dynamically allocated array.
ptr
1/17/2008
CS 3424
91
1/17/2008
CS 3424
92
char* ptr; ptr = new char; *ptr = B; cout << *ptr; delete ptr; ptr = NULL;
1/17/2008 CS 3424
char* ptr;
3000 NULL
ptr = new char; *ptr = B; cout << *ptr; delete ptr; ptr = NULL;
1/17/2008
ptr
94
Check Yourself
void main( ) { int *p1, *p2; p1 = new int; *p1 = 42; p2 = p1; cout << "*p1 == " << *p1 << endl; cout << "*p2 == " << *p2 << endl; *p2 = 53; cout << "*p1 == " << *p1 << endl; cout << "*p2 == " << *p2 << endl; p1 = new int; *p1 = 88; cout << "*p1 == " << *p1 << endl; cout << "*p2 == " << *p2 << endl; *p2 = *p1; cout << "*p1 == " << *p1 << endl; cout << "*p2 == " << *p2 << endl;
1/17/2008
int* ptr = new int; *ptr = 3; ptr ptr = new int; *ptr = 4;
3 ptr 4
CS 3424
95
1/17/2008
CS 3424
96
16
Inaccessible Object
An inaccessible object is an unnamed object that was created by operator new and which a programmer has left without a pointer to it. int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5;
ptr
-5
ptr2 // here the 8 becomes inaccessible
8
ptr
ptr2
-5
ptr2
1/17/2008 CS 3424 97 1/17/2008 CS 3424 98
Memory Leak
A memory leak is the loss of available memory space that occurs when dynamic data is allocated but never deallocated.
A Dangling Pointer
is a pointer that points to dynamic memory that has been deallocated
int* ptr = new int; *ptr = 8; int* ptr2 = new int; *ptr2 = -5; ptr = ptr2;
8
ptr
-5
ptr2
FOR EXAMPLE,
1/17/2008 CS 3424 99 1/17/2008 CS 3424 100
Functions
C++ supports objects with methods, and stand alone functions function prototype must be before call to function implementation may be anywhere
8
ptr
-5
ptr2
delete ptr2;
// ptr is left dangling ptr NULL ptr2
1/17/2008 CS 3424
ptr2 = NULL;
101
1/17/2008
CS 3424
102
17
Functions
A function is a collection of statements that perform some action and/or compute some value.
char min_char(char ch1, char ch2) { if (ch1 <= ch2) return ch1; else return ch2; }
Function Definition
Form:
return-type function-name(parameter list) { function body
}
Function definitions are generally placed in their own file, or related function definitions may be grouped into a single file.
1/17/2008
CS 3424
103
1/17/2008
CS 3424
104
Function Declaration
To use a function within a source file it must be declared. Form:
return-type function-name(parameter list);
Parameters
Three types of parameters: Pass by value (copy of data value)
int pbv (int copy);
Within the parameter list, only the types of the parameters are required.
char min_char(char, char);
1/17/2008
CS 3424
105
1/17/2008
CS 3424
106
4000 25 age
Value Parameter
The value (25) of the argument is passed to the function when it is called. In this case, the argument can be a variable identifier, constant, or expression.
107 1/17/2008
1/17/2008
CS 3424
18
The statement:
swap(i, j);
1/17/2008
CS 3424
Output
p1_value in function call = 111 p2_ref in function call = 222 n1 after function1 call = 1 n2 after function1 call = 222 p1_value in function call = 111 p2_ref in function call = 222 n1 after function1 call = 222 n2 after function1 call = 2
111 1/17/2008 CS 3424 112
void do_stuff (int p1_value, int& p2_ref) { p1_value = 111; cout << p1_value in function call = << p1_value << endl; p2_ref = 222; cout << p2_ref in function call = << p2_ref << endl; }
1/17/2008
CS 3424
Test Yourself
Whats the output? void sum_diff (int, int, //IN int&, int&); // IN-OUT: void main ( ) { int w = 9, x = 5, y = 3, z = 7; cout << x y z w << endl; sum_diff (x, y, z, w); cout << << x << << y << << z << << w << endl; sum_diff (y, x, z, w); cout << << x << << y << << z << << w << endl; sum_diff (z, w, y, x); cout << << x << << y << << z << << w << endl; sum_diff (x, x, y, x); cout << << x << << y << << z << << w << endl; } void sum_diff (int num1, int num2, int& num3, int& num4) { num3 = num1 + num2; num4 = num1 num2; }
1/17/2008 CS 3424 113
1/17/2008
CS 3424
114
19
Overloading
In C++, functions can be overloaded
functions with same name, signature must be different
1/17/2008
CS 3424
115
1/17/2008
CS 3424
116
Operator Functions
C++ allows class types to behave the same as the primitive types. You can define operators such as +, -, * etc. to operate. Example, if s1 and s2 are strings
s1 + s2
Operator Overloading
Statement: a+b Can be interpreted as: a.operator + (b); // + is member function OR operator + (a, b) // + is stand alone
represents the string consisting of s1 followed by s2. The name of the operator functions is the form operator@ where @ represents the operator. Example:
operator+
is the + operator.
1/17/2008
CS 3424
117
1/17/2008
CS 3424
118
Arrays
An array is an object. The elements of an array are all of the same type. The elements of an array are accessed by an index applied to the subscript operator.
array-name[index]
Declaring an array
Form:
type-name array-name[size]; type-name array-name[] = {initialization list};
Examples:
int scores[5]; string names[] = {"Sally", "Jill", "Hal", "Rick"};
1/17/2008
CS 3424
119
1/17/2008
CS 3424
120
20
and
*students
is equivalent to
*(a + i)
and
&a[i]
to
(a + i)
1/17/2008
CS 3424
121
1/17/2008
CS 3424
122
will allocate space for size objects of type typename are return a pointer to the first object. A declaration of the form:
pointer-variable = new type-name[size];
Note that pointer-variable must have been initialized using the new[] operator.
1/17/2008
CS 3424
123
1/17/2008
CS 3424
124
C-Strings
The C programming language uses an array of char values terminated with the null character ('\0'). Thus the constant
"hello"
Example:
int find(int x[], int n, int target); int find(int* x, int n, int target);
are equivalent. You can call this funcion with either an array or a pointer:
int loc = find(scores, 10, 50); int loc = find(scores + 5, 5, 50);
is stored as: h e l l o \0
1/17/2008
CS 3424
125
1/17/2008
CS 3424
126
21
Behavior
1/17/2008
CS 3424
127
1/17/2008
CS 3424
128
Behavior
Search
size_t find(const string& str, size_t pos) const size_t find(const char* s, size_t pos) const size_t find(const char c, size_t pos) const
1/17/2008
CS 3424
129
1/17/2008
CS 3424
130
Function
Search
size_t find_first_not_of(const string& str, size_t pos = 0) const size_t find_first_not_of(const char* s, size_t pos = 0); size_t find_first_not_of(const char* s, size_t pos, size_t n)const find first_not_of(char c, size_t pos = 0) const
Behavior
Returns the index of where any character that is not in the target first occurs in this string, starting the search at pos. If such a character is not found, the value string::npos is returned. If the parameter pos is not specified, 0 is the default. The parameter n indicates the length of the character array pointed to by s. If it is omitted, the character array s is null terminated.
1/17/2008
CS 3424
131
1/17/2008
CS 3424
132
22
Function
Conversion to C String
const char* c_str()
Behavior
Returns a pointer to a nullterminated array of characters that contain the same data as this string. Returns a pointer to an array of characters that contain the data contained in this string. This array is at least size() characters long and is not necessarily null-terminated.
Comparisons
size_t compare(const string& other) Returns 0 if other is equal to this string, 1 if this string is less than the other string, and +1 if this string is greater than the other string. operator==(const string&, const string&) All of the infix comparisons operator!=(const string&, const string&) are defined. operator<(const string&, const string&) operator<=(const string&, const string&) operator>=(const string&, const string&) operator>(const string&, const string&)
1/17/2008
CS 3424
133
1/17/2008
CS 3424
134
searches the string to which it is applied for the substring str. The function
size_t find_first_of(const string& str);
treats str as a set of characters, and will find the first occurrence of a member of str within the string to which it is applied.
will set end to 4, the index of the first ! after position start. The function call
string word = line.substr(start, end start);
1/17/2008
CS 3424
135
1/17/2008
CS 3424
136
Summary
Obviously theres a lot more to C++, but these are the basics that should get you started. Dont worry if it seems like a lot. Through practice youll get used to it.
1/17/2008
CS 3424
137
23