DS in C++ Tim Budd
DS in C++ Tim Budd
Data Structures
in C++
Chapter 1
Tim Budd
Oregon State University
Corvallis, Oregon
USA
Language Features
The purpose of this chapter is to quickly review all those features of
C++ that you should already have encountered and be familiar
with.
Even if you have learned programming in another language (Pascal,
for example) you should be able to quickly get up to speed with the
features described here.
Comments
There are two forms of comments in C++
// from slashes to end of line
/
comments that span
multiple lines
/
Comments should be used extensively for documentation.
Constants
There are various types of constants:
integer { 1, 12, 37
octal integers { 014
hexadecimal integers 0XFF 0XC
oating point { 3.14159 2.7e14
character { 'a' '\n'
string { "abc"
Suxes can be applied to integer constants (U for unsigned, L for
long)
Several other special backslash characters
c = 43;
f = (c 9.0) / 5.0 + 32;
Lots of Operators
Unary Operators
increment, decrement i++, ++i, i , i
negation i
bit-wise inverse i
Arithmetic Operations
addition, subtraction a+b a b
multiplication, division ab a=b
remainder after division a%b
Shift Operations (also stream I/O)
left shift (also stream output) a << b
right shift (also stream input) a >> b
Relational Operations
less than, less than or equal < <=
equal, not equal == ! =
greater than, greater than or equal > >=
Logical Operations
and x && y
or x jj y
logical negation !i
Miscellaneous Operations
function call f(a,b,c)
conditional expression c? a: b
Stream I/O
The left and right shift operators are given dierent meanings when
used with stream values. The most common stream is associated
with \console input and output".
cout "the Fahrenheit equivalent of " c
" is " f "\n";
Pointers
A pointer is a variable that maintains the address of another
location in memory.
PPP
PqP
Conditional Statements
Normal sequential control can be modied using a conditional
statment:
month aMonth;
...
if ((aMonth >= June) && (aMonth <= August))
isSummer = true;
else
isSummer = false;
Switch Statements
Switch statements can select one of many alternatives:
switch (aMonth) f
case January:
highTemp = 20;
lowTemp = 0;
break;
case February:
highTemp = 30;
lowTemp = 10;
break;
...
case July:
highTemp = 120;
lowTemp = 50;
break;
default:
highTemp = 60;
lowTemp = 20;
g;
Loops
Loops are used to execute statements repeatedly until a condition is
satised.
c = 0;
while (c <= 100) f
cout "Celsius " c " is Fahrenheit "
((9.0 c) / 5.0 + 32) "\n";
c += 10;
g
For statements
For statements combine in one statement initialization, termination
test, and update.
for (c = 0; c <= 100; c += 10) f
cout "Celsius " c " is Fahrenheit "
((9.0 c) / 5.0 + 32) "\n";
g
Declaration of new variables can be combined with loop.
for (int i = 0; i < 12; i++) f
cout "i: " i " i squared " ii "\n";
g
Array
An array is a xed sized collection of similarly-typed values. Array
elements are accessed using subscripts, range is zero to one less
than array size.
// declare an array of twelve integer values
int Temperatures[12];
// now assign all values
Temperatures[0] = 0;
Temperatures[1] = 10;
Temperatures[2] = Temperatures[1] + 15;
...
Multidimensional Arrays
Arrays of more than one dimension can be created by giving the
extent along each axis.
double matrix[10][20];
Arrays as Arguments
When used as an argument, size need not be specied:
int arraySum (int values[ ], int n)
// compute sum of array values[0] .. values[n-1]
f
int result = 0;
for (int i = 0; i < n; i++) f
result += values[i]
g
return result;
g
Structures
A structure is a collection of elds, which need not have the same
type.
struct person f
string name;
int age;
enum fmale, femaleg sex;
g;
Fields are accessed using dot notation.
person employee;
employee.name = "sam smith";
employee.age++;
if (employee.sex == male)
...
Functions
Functions encapsulate a set of actions, so that later we can refer to
the sequence of actions by name alone:
int Fahrenheit(int cTemp)
f
return (cTemp 9.0) / 5.0 + 32;
g
Parts:
Header { with return type, name, and arguments
Body { with statements to execute. Can have return statement
to end execution.
Return type can be void { no value.
A function prototype is a declaration but not a denition, just gives
name, arguments and return type.
// prototype for Fahrenheit { denition occurs later
int Fahrenheit (int);
Local Variables
Variables within a function come into existance when the function
is entered, disappear when the function exits. Execute in stack-like
fashion. Assume function A calls function B which calls function C
{ can imagine variables as follows:
local variables for C
local variables for B
local variables for A
void main() f
// program to write table of squares
cout "Table of Squares\n";
Include Files
Many data structures require one to dene an include le before
they can be processed.
purpose name
stream input/output iostream
math functions math.h
complex numbers complex
Boolean values bool.h
generic algorithms algorithm