C++ Lecture 8
[Previous Lecture]
[Lecture Index]
L.h
#ifndef LIST_H
# define LIST_H
class List {
struct ListLink {
ListLink(int v) : val(v), next(0) { }
int val;
ListLink *next;
};
public:
typedef struct ListLink *Iterator;
List();
List(const List &ic);
~List();
Iterator getIterator() const;
bool getNext(Iterator &iter, int &val)
const;
void add(int val);
bool remove(const int &val);
void removeAll();
private:
ListLink *head;
ListLink *tail;
};
#endif /* LIST_H */
L.cc
// ...
bool
List::getNext(Iterator &iter, int &val) const {
if (!iter)
return false;
val = iter->val;
iter = iter->next;
return true;
}
void
List::add(int val)
{
ListLink *n = new ListLink(val);
if (tail)
tail->next = n;
else
head = tail = n;
tail = n;
}
Lt.h
#ifndef LIST_H
# define LIST_H
template <class T>
class List {
struct ListLink {
ListLink(T v) : val(v), next(0) { }
T val;
ListLink *next;
};
public:
typedef struct ListLink *Iterator;
List();
List(const List &ic);
~List();
Iterator getIterator() const;
bool getNext(Iterator &iter, T &val)
const;
void add(T val);
bool remove(const T &val);
void removeAll();
private:
ListLink *head;
ListLink *tail;
};
#endif /* LIST_H */
Lt.h (continued)
// ...
template <class T>
bool
List<T>::getNext(Iterator &iter, T &val) const
{
if (!iter)
return false;
val = iter->val;
iter = iter->next;
return true;
}
template <class T>
void
List<T>::add(T val)
{
ListLink *n = new ListLink(val);
if (tail)
tail->next = n;
else
head = tail = n;
tail = n;
}
Streams and Formatting: methods
-
width(int w): set the width
width(): get the current width
- field width of next number or char *
(not char);
may be used by user defined output operators.
- effects next output only
- width of 0 means as many as needed (the default)
the following effect all subsequent output
-
precision(int): set the precision
precision(): get the current precision
- sets precision of floating point numbers (how many digits
to print)
- default is 6
-
fill(char c): set
fill(): get
- sets fill character used to pad out fields
(default is space)
-
flags(ios::fmtflags f): set
flags(): get
-
setf(ios::fmtflags f)
- or f into the existing flags
-
setf(ios::fmtflags f, ios::fmtflags mask)
- clear the mask bits, set the f bits
- mask values are
- ios::adjustfield - left, right, internal
- ios::basefield - dec, oct, hex
- ios::floatfield - scientific, fixed
-
unsetf(ios::fmtflags f)
- clear the flags from the existing flags
Streams and Formatting: manipulators
`manipulators' allow formating in a << sequence
(a bit more convenient)
[Previous Lecture]
[Lecture Index]