• Keine Ergebnisse gefunden

Object-Oriented Programming for Scientific Computing

N/A
N/A
Protected

Academic year: 2021

Aktie "Object-Oriented Programming for Scientific Computing"

Copied!
48
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Object-Oriented Programming for Scientific Computing

Templates and Static Polymorphism

Ole Klein

Interdisciplinary Center for Scientific Computing Heidelberg University

ole.klein@iwr.uni-heidelberg.de

2. Juni 2015

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 1 / 48

(2)

Keyword typename

t e m p l a t e<t y p e n a m e T , int d i m e n s i o n = 3 >

c l a s s N u m e r i c a l S o l v e r {

...

p r i v a t e:

t y p e n a m e T :: S u b T y p e v a l u e _ t y p e ; }

• Template classes often define types (e.g. to determine the return type of functions as a function of the template parameters).

• A C++ compiler can not know what the constructT::Nameis (hereTis a typename template argument), as it does not yet know the class definition of

T. It therefore assumes by default that this is a static variable.

• If it is a type defined in the class instead, then this must be clearly specified with the keywordtypename.

• This is only required within function or class templates (otherwise it is clear what exactly means).

(3)

Member Templates

Class members (methods or nested classes) can be templates as well.

t e m p l a t e<t y p e n a m e T>

c l a s s S t a c k {

p r i v a t e:

s t d : : deque<T> e l e m s ; p u b l i c:

v o i d p u s h (c o n s t T&) ; v o i d pop ( ) ;

T t o p ( ) c o n s t; b o o l empty ( ) c o n s t {

r e t u r n e l e m s . empty ( ) ; }

// a s s i g n m e n t o f s t a c k o f e l e m e n t s o f t y p e T2 t e m p l a t e<t y p e n a m e T2>

S t a c k<T>&o p e r a t o r=(c o n s t S t a c k<T2>&) ; };

In this example, the default assignment operator is overloaded, not replaced (see the rules for overloading template functions).

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 3 / 48

(4)

Member Templates

t e m p l a t e<t y p e n a m e T>

t e m p l a t e<t y p e n a m e T2>

S t a c k<T>& S t a c k<T>: :o p e r a t o r=(c o n s t S t a c k<T2>& o t h e r ) {

i f( (v o i d)t h i s==(v o i d)&o t h e r ) r e t u r n t h i s;

S t a c k<T2> tmp ( o t h e r ) ; e l e m s . c l e a r ( ) ; w h i l e( ! tmp . empty ( ) ) {

e l e m s . p u s h f r o n t ( tmp . t o p ( ) ) ; tmp . pop ( ) ;

}

r e t u r n t h i s; }

• We now need twotemplatelines at the start of the method definition.

• AsStack<T>andStack<T2>are completely different types, one can only use

(5)

Member Templates

Usage:

int m a i n (int argc , c h a r** a r g v ) {

Stack <int> i n t S t a c k ; Stack <float> f l o a t S t a c k ; i n t S t a c k . p u s h ( 1 0 0 ) ; f l o a t S t a c k . p u s h ( 0 . 0 ) ; f l o a t S t a c k . p u s h ( 1 0 . 0 ) ;

f l o a t S t a c k = i n t S t a c k ; // OK , int c o n v e r t s to f l o a t i n t S t a c k = f l o a t S t a c k ; // h e r e i n f o r m a t i o n may be l o s t }

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 5 / 48

(6)

Keyword .template

c l a s s A {

p u b l i c:

t e m p l a t e<c l a s s T>T d o S o m e t h i n g ( ) { };

};

t e m p l a t e<c l a s s U> v o i d d o S o m e t h i n g E l s e (U v a r i a b l e ) {

c h a r r e s u l t = v a r i a b l e .t e m p l a t e d o S o m e t h i n g<c h a r>() ; }

t e m p l a t e<c l a s s U ,t y p e n a m e V>V d o S o m e t h i n g M o r e (U v a r i a b l e ) {

r e t u r n v a r i a b l e>t e m p l a t e d o S o m e t h i n g<V>() ; }

• Another ambiguity concerns the<character. A C++ compiler assumes by default that the sign<marks the beginning of a comparison.

• With templates this results in cryptic error messages like

error: expected primary-expression before ....

(7)

Template Template Parameters

• It may be necessary for a template parameter to be itself a class template.

• In the Stack class with interchangeable container, the user must specify the used type of container him/herself.

S t a c k<i n t , s t d : : v e c t o r<i n t> > myStack ; In case the two types don’t match, this is error-prone.

• It is better to write this with a template template parameter:

t e m p l a t e<t y p e n a m e T , t e m p l a t e<typename> c l a s s C=s t d : : deque>

c l a s s S t a c k{ p r i v a t e:

C<T> e l e m s ; . . .

}

• Usage:

S t a c k<i n t , s t d : : v e c t o r> myStack ;

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 7 / 48

(8)

Template Template Parameters

• Within the class, template template parameters can be instantiated with any type, not just with one of the template parameters of the class.

• The template template argument must exactly match the template template parameter for which it is used. Here default values aren’t applied.

(9)

Stack with Template Template Parameter

t e m p l a t e<t y p e n a m e T , t e m p l a t e<t y p e n a m e U ,

t y p e n a m e = std :: a l l o c a t o r < U > >

c l a s s C = std :: deque >

c l a s s S t a c k {

p r i v a t e: C < T > e l e m s ; p u b l i c:

v o i d p u s h (c o n s t T &) ; v o i d pop () ;

T top () c o n s t; b o o l e m p t y () c o n s t {

r e t u r n e l e m s . e m p t y () ; }

// a s s i g n m e n t of s t a c k of e l e m e n t s of t y p e T2

t e m p l a t e<t y p e n a m e T2 , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C2 >

Stack < T , C >& o p e r a t o r=(c o n s t Stack < T2 , C2 >&) ; };

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 9 / 48

(10)

Stack with Template Template Parameter II

t e m p l a t e<t y p e n a m e T , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C >

v o i d Stack < T , C >:: p u s h (c o n s t T & e l e m ) {

e l e m s . p u s h _ b a c k ( e l e m ) ; }

t e m p l a t e<t y p e n a m e T , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C >

v o i d Stack < T , C >:: pop () {

if( e l e m s . e m p t y () )

t h r o w std :: o u t _ o f _ r a n g e (" Stack < >:: pop () : e m p t y s t a c k ") ; e l e m s . p o p _ b a c k () ;

}

t e m p l a t e<t y p e n a m e T , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C >

T Stack < T , C >:: top () c o n s t {

if( e l e m s . e m p t y () )

t h r o w std :: o u t _ o f _ r a n g e (" Stack < >:: pop () : e m p t y s t a c k ") ;

(11)

Stack with Template Template Parameter III

t e m p l a t e<t y p e n a m e T , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C >

t e m p l a t e<t y p e n a m e T2 , t e m p l a t e<t y p e n a m e, t y p e n a m e> c l a s s C2 >

Stack < T , C >& Stack < T , C >::o p e r a t o r=(c o n s t Stack < T2 , C2 >& o t h e r ) {

if((v o i d*)t h i s==(v o i d*) & o t h e r ) r e t u r n *t h i s;

Stack < T2 , C2 > tmp ( o t h e r ) ; e l e m s . c l e a r () ;

w h i l e(! tmp . e m p t y () ) {

e l e m s . p u s h _ f r o n t ( tmp . top () ) ; tmp . pop () ;

}

r e t u r n *t h i s; }

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 11 / 48

(12)

Stack with Template Template Parameter IV

Usage:

int m a i n (int argc , c h a r** a r g v ) {

Stack <int> i n t S t a c k ;

Stack <float, std :: deque > f l o a t S t a c k ; i n t S t a c k . p u s h ( 1 0 0 ) ;

f l o a t S t a c k . p u s h ( 0 . 0 ) ; f l o a t S t a c k . p u s h ( 1 0 . 0 ) ;

f l o a t S t a c k = i n t S t a c k ; // OK , int c o n v e r t s to f l o a t i n t S t a c k = f l o a t S t a c k ; // h e r e i n f o r m a t i o n may be l o s t }

(13)

Initialization with Zero

• In C++ the variables of builtin types (such asint,double, or pointers) won’t be initialized with default values for performance reasons.

• Each uninitialized variable has undefined content (the random entries of the memory location):

t e m p l a t e<t y p e n a m e T >

v o i d foo () {

T x ; // x has u n d e f i n e d v a l u e if T is a built - in t y p e }

• However, it is possible to explicitly invoke a default constructor for built-in types that sets the variable to zero (orfalsein the case of the typebool)

t e m p l a t e<t y p e n a m e T >

v o i d foo () {

T x () ; // x is z e r o ( or f a l s e ) w h e n T is a built - in t y p e }

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 13 / 48

(14)

Initialization with Zero

• If it should be ensured that all the variables in a class template are going to be inintialized, then a constructor has to be explicitly called for all attributes in the initialization list.

t e m p l a t e<t y p e n a m e T >

c l a s s M y C l a s s {

p r i v a t e: T x ; p u b l i c:

M y C l a s s () : x () // i n i t i a l i z e s x {

} ...

};

(15)

C++11: Template Aliases

t e m p l a t e <t y p e n a m e T , int U >

c l a s s G e n e r a l T y p e {};

t e m p l a t e <int U > // for p a r t i a l l y d e f i n e d t e m p l a t e s u s i n g I n t N a m e = G e n e r a l T y p e <int, U >;

int m a i n () {

u s i n g i n t 3 2 = int; // for n o r m a l t y p e s

u s i n g F u n c t i o n = v o i d (*) (d o u b l e) ; // for f u n c t i o n s

u s i n g S p e c i a l T y p e = G e n e r a l T y p e <int,36 >; // for f u l l y d e f i n e d t e m p l a t e s

IntName <7 > foo ; }

• In C ++11, there is an alternative method totypedefs to define abbreviations for long type names.

• This alternative is called “template aliasing”.

• It also allows to set some of the template arguments.

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 15 / 48

(16)

Independent Base Classes

• An independent base class is fully determined even without knowledge of a template parameter.

• Independent base classes behave essentially as base classes in normal (non-template) classes.

• If a name appears in the class but no namespace precedes it (an unqualified type), then the compiler will look in the following order for a definition:

1 Definitions in the class

2 Definitions in independent base classes

3 Template arguments

(17)

Independent Base Classes

t e m p l a t e<t y p e n a m e X>

c l a s s B a s e {

p u b l i c:

i n t b a s e f i e l d ; t y p e d e f i n t T ; };

c l a s s D1 : p u b l i c Base<Base<v o i d> >

{

p u b l i c: v o i d f ( ) {

b a s e f i e l d = 3 ; // A c c e s s t o i n h e r i t e d number }

};

t e m p l a t e<c l a s s T>

c l a s s D2 : Base<d o u b l e>

{ // i n d e p e n d e n t b a s e c l a s s p u b l i c:

v o i d f ( ) {

b a s e f i e l d = 7 ; // A c c e s s t o i n h e r i t e d number }

T s t r a n g e ; // T h a s t y p e Base<d o u b l e>: :T ! ! };

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 17 / 48

(18)

Independent Base Classes

int m a i n (int argc , c h a r** a r g v ) {

D1 d1 ; d1 . f () ;

D2 <double> d2 ; d2 . f () ;

d2 . s t r a n g e =1;

d2 . s t r a n g e = 1 . 1 ; // B e w a r e : d2 . s t r a n g e has t y p e int ! std :: c o u t < < d2 . s t r a n g e < < std :: e n d l ;

}

(19)

Dependent Base Classes

• In the last example, the base class was completely defined.

• This does not apply for base classes which depend on an a template paramater.

• The C++ standard dictates that independent names appearing in a template are resolved at their first occurrence.

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 19 / 48

(20)

Dependent Base Classes

t e m p l a t e<t y p e n a m eT>

c l a s s DD : p u b l i c Base<T>

{ p u b l i c:

v o i d f ( ) {

b a s e f i e l d = 0 ; // ( 1 ) w o u l d l e a d t o t y p e r e s o l u t i o n and b i n d i n g t o i n t . }

};

t e m p l a t e<>

c l a s s Base<b o o l>

{ p u b l i c:

enum{ b a s e f i e l d = 42}; // ( 2 ) T e m p l a t e s p e c i a l i z a t i o n w a n t s t o // d e f i n e v a r i a b l e d i f f e r e n t l y . };

v o i d g (DD<b o o l>& d ) {

d . f ( ) // ( 3 ) C o n f l i c t }

1 In the definition of classDD, the first access tobasefieldin f()would lead to bindingbasefieldtoint(because of the definition in the class template).

2 Subsequently, however, the type ofbasefieldwould be modified into

(21)

Solution: Delayed Type Resolution

• In order to prevent this problem from happening, C++ defines that independent names won’t be searched in dependent base classes. The C++

compiler stops already at (1) and displays an error message (error: ’basefield’was not declared in this scope).

• The base class attributes and methods must therefore be prefixed by either

this->” or “Base<T>::”.

• As a result, the name is dependent and so will only be resolved during instantiation.

• Example

t e m p l a t e<t y p e n a m e T >

c l a s s DD : p u b l i c Base < T >

{

p u b l i c: v o i d f () {

this- > b a s e f i e l d = 0;

} };

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 21 / 48

(22)

Solution: Delayed Type Resolution

or

t e m p l a t e<t y p e n a m e T >

c l a s s DD : p u b l i c Base < T >

{

p u b l i c: v o i d f () {

Base < T >:: b a s e f i e l d = 0;

} };

or short:

t e m p l a t e<t y p e n a m e T >

c l a s s DD : p u b l i c Base < T >

{

u s i n g Base < T >:: b a s e f i e l d ; // (1) is now d e p e n d e n t // for w h o l e c l a s s

(23)

Unified Modeling Language (UML)

• Class hierarchies can be quite complex.

• It is useful to be able to represent them graphically.

• The Unified Modeling Language (UML) is the standard. It is used for visualization, specification, construction and documentation of object-oriented software.

• It is the result of several predecessors (e.g. Booch, OOSE and OMT).

• Version 1.0 was released in September 1997.

• There are also plugins for development environments (e.g. Eclipse) that are used to automatically generate code from UML diagrams.

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 23 / 48

(24)

UML Diagram Types

UML supplies 9 different kind of diagram:

1 Class diagram

2 Object

3 Use case

4 Sequence

5 Collaboration

6 Statechart

7 Activity

8 Component

9 Deployment

We treat only a tiny part of this extensive tool set.

(25)

Classes

• Classes are represented by rectangular boxes, which are divided into different sections by horizontal lines.

• The name of the class comes first, followed by its attributes and then its methods.

Element

domain Domain()

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 25 / 48

(26)

Attributes

• Attributes can have a type and optionally a default value.

Wall height : float width : float

carrying : bool = false

(27)

Access Control

• The access rights to attributes are expressed by the leading characters+for

public,#forprotectedand-forprivate. StackInt

# current : int - size : int + Top(): int + Push(): int - Resize(s: int)

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 27 / 48

(28)

Static Class Members

• Static class members are indicated by underlining.

Wall height : float width : float

carrying : bool = false thickness : int

(29)

Inheritance

• When a class is derived from another one, this is marked by a line connecting the two classes, with an unfilled triangle at the base class opening itself in the direction of the derived class:

Element

domain Domain() Nodes()

Triangle Quadrilateral

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 29 / 48

(30)

Interface Base Classes, Purely Virtual Functions

• Interface base classes are characterized by the line<<interface>>.

• The connection to classes which implement the interface are like normal inheritance but dashed.

interface Integrator

+ operator(f: Functor&): double

MidpointRule - a : double

- b : double - n : size t

+ MidpointRule(a: double, b: double,

SimpsonRule - a : double

- b : double - n : size t

+ SimpsonRule(a: double, b: double,

(31)

Association

The association between two elements is represented by a connecting line.

Numbers on the line can specify the number of connections and a name for the connection. An association means that there is a relationship between two elements such as between a bank account and a customer.

A is associated with a B.

A 1 B

A is associated with one or more B.

1 ..∗

A B

A is associated with none or one B.

0 .. 1

A B

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 31 / 48

(32)

Association

A is associated with none, one or several B.

A B

An association may have a name:

b 1

A B

e.g.

c l a s s B ; c l a s s A {

B * b ; };

and there is also association in both directions:

(33)

Dependencies

A class can depend on another, e.g. because it is afriend:

<<friends>>

A B

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 33 / 48

(34)

Composition

Composition is represented by an interconnecting line with a filled lozenge on the side of the composite class. A composition consists of parts which belong to a whole and for which it bears the responsibility. Compositions are usuallynto one relationships.

1

4 Car

Wheel

(35)

Aggregation

Aggregation is represented by connecting lines with an empty lozenge on the side of the aggregated class. Aggregation is a somewhat stronger relationship than association, but in contrast to composition dependent objects won’t automatically be destroyed with the main object. A university for example can be represented as a composition of faculties, but these are only aggregations of professors.

3 ..∗

1

1

1 Polygon

Point

Style color: unsigned char isFilled: bool

Circle

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 35 / 48

(36)

Templates in UML

t e m p l a t e<t y p e n a m e T >

c l a s s Set {

v o i d i n s e r t ( T e l e m e n t ) ; };

Set

insert(T) T

(37)

Templates in UML

Realisation ofSetwithT=int.

Set<int>

insert(T)

or

<<bind>><T→int>

Set

insert(T) T

IntSet

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 37 / 48

(38)

The Standard Template Library(STL)

• The Standard Template Library (STL)

The Standard Template Library is a class library for diverse needs.

It provides algorithms to work with these classes.

• It also formulates interfaces, which must be provided by other collections of classes in order to be used as STL classes, or can be used to write algorithms that work with all STL-like container classes.

• The STL is a new level of abstraction, which frees the programmer from the necessity to write frequently used constructs such as dynamic arrays, lists, binary trees, search algorithms, and so on himself.

• STL algorithms are programmed as optimally as possible, i.e. if there is an STL algorithm for a problem, one should have a very good reason not to use it.

• Unfortunately, the STL is not self-explanatory.

(39)

STL Components

• The main components of the STL are:

Containers are used to manage a particular type of object. The various containers have different properties and related advantages and disadvantages. The containers that fit best should be used in any given situation.

Iterators make it possible to iterate over the contents of a container.

They provide a uniform interface for each STL compliant container, regardless of its internal structure.

Algorithms work with the elements of a container. They use iterators and therefore must only be written once for an arbitrary number of STL-compliant containers.

• At first sight, the structure of the STL partially contradicts the original idea of object-oriented programming that algorithms and data belong together.

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 39 / 48

(40)

Containers

STL container classes, or short containers, manage a collection of elements of the same type. Depending on the type of container, the STL gives assurances on the execution speed of certain operations.

There are two fundamentally different types of container:

Sequences are ordered sets of elements with freely selectable arrangement.

Each element has its place, which depends on the program execution and not on the value of the element.

Associative Containers are ordered sets sorted according to a certain sorting criterion in which the position of an element depends only on its value.

(41)

Vector

STL sequence containers are class templates. There are two template arguments, the type of objects to be stored and a so-called allocator that can be used to change the memory management (this is useful for example when you create many small objects and don’t want to pay the operating system overhead every time).

The second parameter has a default value wherenew()anddelete()are used.

Vector is a field of variable size.

• Adding and removing elements at the end of a vectoris fast, i.e. complexityO(1).

• The element can be accessed directly via an index (random access).

Abbildung:Structure of avector

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 41 / 48

(42)

Amortized Complexity

• Typically, adding elements at the end of astd::vectoris inO(1).

• In individual cases, however, it may take much longer, especially if the allocated storage is no longer sufficient. Then new storage must be allocated, and often data has to be copied over. This is anO(N) process.

• However, the standard library reserves memory blocks of increasing size for a growing vector. The overhead depends on the length of the vector. This optimizes the speed at the expense of memory usage.

• TheO(N)-case therefore occurs very rarely.

• This is called “amortized complexity”.

• If it is already known that a certain amount of elements is needed, then one can reserve space with the methodreserve(size_t size). This doesn’t change the current size of the vector, it only reserves the right amount of memory.

(43)

Example: STL Vector

# i n c l u d e < i o s t r e a m >

# i n c l u d e < vector >

# i n c l u d e < string >

int m a i n () {

std :: vector <double> a (7) ;

std :: c o u t < < a . s i z e () < < std :: e n d l ; for (int i =0; i < 7 ; + + i )

a [ i ] = i * 0 . 1 ; d o u b l e d = 4 * a [ 2 ] ; std :: vector <double> c ( a ) ;

std :: c o u t < < a . b a c k () < < " " < < c . b a c k () < < std :: e n d l ; std :: vector < std :: string > b ;

b . r e s i z e (3) ;

for (int i =2; i >=0; - - i ) std :: cin > > b [ i ];

b . r e s i z e (4) ; b [3] = " b l u b ";

b . p u s h _ b a c k (" b l o b ") ;

for (int i =0; i < b . s i z e () ;++ i ) std :: c o u t < < b [ i ] < < std :: e n d l ; }

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 43 / 48

(44)

Deque

Deque is a “double-ended” queue, it is also a field of dynamic size, but:

• The addition and removal of elements is quick also at the beginning ofdeque, that isO(1).

• Element access can again be achieved using an index, but the index of an element may change when elements are added to the beginning of the container.

Abbildung:Structure of adeque

(45)

List

List is a doubly linked list of elements.

• There is no direct access to list elements.

• To reach the 10th element, one must start at the beginning of thelistand traverse the first nine elements, access to a specific element is thereforeO(N).

• Adding and removing elements is fast in any location in the

list, i.e.O(1).

Abbildung:Structure of alist

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 45 / 48

(46)

Example: STL List

# i n c l u d e < i o s t r e a m >

# i n c l u d e < list >

# i n c l u d e < string >

int m a i n () {

std :: list <double> v a l s ; for (int i =0; i < 7 ; + + i )

v a l s . p u s h _ b a c k ( i * 0 . 1 ) ; v a l s . p u s h _ f r o n t ( -1) ;

std :: list <double> c o p y ( v a l s ) ;

std :: c o u t < < v a l s . b a c k () < < " " < < c o p y . b a c k () < < std :: e n d l ; std :: c o u t < < v a l s . f r o n t () < < " " < < c o p y . f r o n t () < < std :: e n d l ; for (int i =0; i < v a l s . s i z e () ;++ i )

{

std :: c o u t < < i < < " : " < < v a l s . f r o n t () < < " " < < v a l s . s i z e () < <

std :: e n d l ; v a l s . p o p _ f r o n t () ; }

std :: c o u t < < std :: e n d l ;

for (int i =0; i < c o p y . s i z e () ;++ i ) {

std :: c o u t < < i < < " : " < < c o p y . b a c k () < < " " < < c o p y . s i z e () < <

(47)

C++11: Array

Array is a C++11 replacement for the classical C arrays, i.e. a field of fixed size.

• Thestd::arrayhat two template parameters, the type of the stored objects and the number of elements of the container

• Adding and removing elements is not possible.

• Element access can be achieved directly via index.

• In contrast to C arrays, astd::arrayknows its size and can be used as the other STL containers.

Abbildung:Structure of anarray

Ole Klein (IWR) Object-Oriented Programming 2. Juni 2015 47 / 48

(48)

Example: STL Array

# i n c l u d e < i o s t r e a m >

# i n c l u d e < array >

# i n c l u d e < string >

int m a i n () {

std :: array <double,7 > a ;

std :: c o u t < < a . s i z e () < < std :: e n d l ; for (int i =0; i < 7 ; + + i )

a [ i ] = i * 0 . 1 ; d o u b l e d = 4 * a [ 2 ] ; std :: array <double,7 > c ( a ) ;

std :: c o u t < < a . b a c k () < < " " < < c . b a c k () < < std :: e n d l ; std :: array < std :: string ,4 > b ;

for (int i =2; i >=0; - - i ) std :: cin > > b [ i ];

b [3] = " b l u b ";

for (int i =0; i < b . s i z e () ;++ i ) std :: c o u t < < b [ i ] < < std :: e n d l ; }

Referenzen

ÄHNLICHE DOKUMENTE

replace_copy(b,e,out,v,v2) Create copy of all elements in range [b:e) re- placing elements which are equal to v with v2 , return iterator to end of copy.

• Traits can be used to specify types and values that depend on one or more template parameters. • Policies can be used to specify parts of algorithms as

Since the units are only used as a template parameter, this only affects the class type but does not require memory... Example: Numbers

This header also contains functions that provide a thread ID and the functionality to detach threads that afterwards run as a separate program (fork).. Mutual Exclusion The header

and no exercise group, because things are still being set up, but you are welcome to attend if you have questions about the lecture or exercises or something else to discuss.. E

~List (); // clean up the list and all nodes Node* first() const; // return a pointer to the first entry Node* next( const Node* n) const; // return a pointer to the node after n

Please modify your implementation again to obtain a doubly linked list: each element should also point to its predecessor.. What is

To understand why the size of empty classes (according to standard) is as observed, consider the following class!. struct