11. felder (arrays) ii - eth z
TRANSCRIPT
![Page 1: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/1.jpg)
11. Felder (Arrays) II
Strings, Lindenmayer-Systeme, Mehrdimensionale Felder, Vektorenvon Vektoren, Kürzeste Wege, Felder und Vektoren alsFunktionsargumente
310
![Page 2: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/2.jpg)
Texte
können mit dem Typ std::string aus der Standardbibliothekrepräsentiert werden.
std::string text = "bool";
definiert einen String der Länge 4
311
![Page 3: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/3.jpg)
Texte
können mit dem Typ std::string aus der Standardbibliothekrepräsentiert werden.
std::string text = "bool";
definiert einen String der Länge 4
311
![Page 4: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/4.jpg)
Strings: gepimpte char-Felder
Ein std::string. . .
kennt seine Länge
text.length()
312
![Page 5: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/5.jpg)
Strings: gepimpte char-Felder
Ein std::string. . .kann mit variabler Länge initialisiert werden
std::string text (n, ’a’)
312
![Page 6: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/6.jpg)
Strings: gepimpte char-Felder
Ein std::string. . .„versteht” Vergleiche
if (text1 == text2) ...
312
![Page 7: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/7.jpg)
Lindenmayer-Systeme (L-Systeme)
Fraktale aus Strings und Schildkröten
313
![Page 8: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/8.jpg)
Definition und Beispiel
Alphabet Σ
Σ∗: alle endlichen Wörterüber Σ
Produktion P : Σ→ Σ∗
Startwort s0 ∈ Σ∗
{F , + , −}
c P (c)F F + F ++ +− −
F
DefinitionDas Tripel L = (Σ, P, s0) ist ein L-System.
314
![Page 9: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/9.jpg)
Definition und Beispiel
Alphabet Σ
Σ∗: alle endlichen Wörterüber Σ
Produktion P : Σ→ Σ∗
Startwort s0 ∈ Σ∗
{F , + , −}
c P (c)F F + F ++ +− −
F
DefinitionDas Tripel L = (Σ, P, s0) ist ein L-System.
314
![Page 10: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/10.jpg)
Definition und Beispiel
Alphabet Σ
Σ∗: alle endlichen Wörterüber Σ
Produktion P : Σ→ Σ∗
Startwort s0 ∈ Σ∗
{F , + , −}c P (c)F F + F ++ +− −
F
DefinitionDas Tripel L = (Σ, P, s0) ist ein L-System.
314
![Page 11: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/11.jpg)
Definition und Beispiel
Alphabet Σ
Σ∗: alle endlichen Wörterüber Σ
Produktion P : Σ→ Σ∗
Startwort s0 ∈ Σ∗
{F , + , −}c P (c)F F + F ++ +− −
F
DefinitionDas Tripel L = (Σ, P, s0) ist ein L-System.
314
![Page 12: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/12.jpg)
Definition und Beispiel
Alphabet Σ
Σ∗: alle endlichen Wörterüber Σ
Produktion P : Σ→ Σ∗
Startwort s0 ∈ Σ∗
{F , + , −}c P (c)F F + F ++ +− −
F
DefinitionDas Tripel L = (Σ, P, s0) ist ein L-System.
314
![Page 13: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/13.jpg)
Die beschriebene Sprache
Wörter w0, w1, w2, . . . ∈ Σ∗: P ( F ) = F + F +
w0 := s0
w1 := P (w0)
w2 := P (w1)
...
w0 := F
w1 := F + F +
w2 := F + F + + F + F + +
...DefinitionP (c1c2 . . . cn) := P (c1)P (c2) . . . P (cn)
315
![Page 14: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/14.jpg)
Die beschriebene Sprache
Wörter w0, w1, w2, . . . ∈ Σ∗: P ( F ) = F + F +
w0 := s0
w1 := P (w0)
w2 := P (w1)
...
w0 := F
w1 := F + F +
w2 := F + F + + F + F + +
...DefinitionP (c1c2 . . . cn) := P (c1)P (c2) . . . P (cn)
315
![Page 15: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/15.jpg)
Die beschriebene Sprache
Wörter w0, w1, w2, . . . ∈ Σ∗: P ( F ) = F + F +
w0 := s0
w1 := P (w0)
w2 := P (w1)
...
w0 := F
w1 := F + F +
w2 := F + F + + F + F + +
...
DefinitionP (c1c2 . . . cn) := P (c1)P (c2) . . . P (cn)
315
![Page 16: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/16.jpg)
Die beschriebene SpracheWörter w0, w1, w2, . . . ∈ Σ∗: P ( F ) = F + F +
w0 := s0
w1 := P (w0)
w2 := P (w1)
...
w0 := F
w1 := F + F +
w2 := F + F + + F + F + +
...
DefinitionP (c1c2 . . . cn) := P (c1)P (c2) . . . P (cn)
F F
P ( F ) P ( F )
+ +
P ( + ) P ( + )
315
![Page 17: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/17.jpg)
Die beschriebene Sprache
Wörter w0, w1, w2, . . . ∈ Σ∗: P ( F ) = F + F +
w0 := s0
w1 := P (w0)
w2 := P (w1)
...
w0 := F
w1 := F + F +
w2 := F + F + + F + F + +
...
DefinitionP (c1c2 . . . cn) := P (c1)P (c2) . . . P (cn)
315
![Page 18: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/18.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts
X
+ : Drehe dich um90 Grad
X
− : Drehe dich um−90 Grad
X
316
![Page 19: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/19.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts
X
+ : Drehe dich um90 Grad
X
− : Drehe dich um−90 Grad
X
316
![Page 20: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/20.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts
X
+ : Drehe dich um90 Grad
X
− : Drehe dich um−90 Grad
X
316
![Page 21: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/21.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts X
+ : Drehe dich um90 Grad
X
− : Drehe dich um−90 Grad
X
Spur
316
![Page 22: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/22.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts X
+ : Drehe dich um90 Grad X
− : Drehe dich um−90 Grad
X
316
![Page 23: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/23.jpg)
Turtle-GrafikSchildkröte mit Position und Richtung
Schildkröte versteht 3 Befehle:F : Gehe einenSchritt vorwärts X
+ : Drehe dich um90 Grad X
− : Drehe dich um−90 Grad X
316
![Page 24: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/24.jpg)
Worter zeichnen!
w1 = F + F +
317
![Page 25: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/25.jpg)
Worter zeichnen!
w1 = F + F +
317
![Page 26: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/26.jpg)
Worter zeichnen!
w1 = F + F +
317
![Page 27: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/27.jpg)
Worter zeichnen!
w1 = F + F +
317
![Page 28: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/28.jpg)
Worter zeichnen!
w1 = F + F +
317
![Page 29: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/29.jpg)
Worter zeichnen!
w1 = F + F +X
317
![Page 30: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/30.jpg)
lindenmayer.cpp: HauptprogrammWörter w0, w1, w2, . . . wn ∈ Σ∗: std::string
...#include "turtle.h"...std::cout << "Number of iterations =? ";unsigned int n;std::cin >> n;
std::string w = "F";
for (unsigned int i = 0; i < n; ++i)w = next_word (w);
draw_word (w);318
![Page 31: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/31.jpg)
lindenmayer.cpp: HauptprogrammWörter w0, w1, w2, . . . wn ∈ Σ∗: std::string
...#include "turtle.h"...std::cout << "Number of iterations =? ";unsigned int n;std::cin >> n;
std::string w = "F";
for (unsigned int i = 0; i < n; ++i)w = next_word (w);
draw_word (w);
w = w0 = F
318
![Page 32: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/32.jpg)
lindenmayer.cpp: HauptprogrammWörter w0, w1, w2, . . . wn ∈ Σ∗: std::string
...#include "turtle.h"...std::cout << "Number of iterations =? ";unsigned int n;std::cin >> n;
std::string w = "F";
for (unsigned int i = 0; i < n; ++i)w = next_word (w);
draw_word (w);
w = wi → w = wi+1
318
![Page 33: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/33.jpg)
lindenmayer.cpp: HauptprogrammWörter w0, w1, w2, . . . wn ∈ Σ∗: std::string
...#include "turtle.h"...std::cout << "Number of iterations =? ";unsigned int n;std::cin >> n;
std::string w = "F";
for (unsigned int i = 0; i < n; ++i)w = next_word (w);
draw_word (w); Zeichne w = wn!318
![Page 34: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/34.jpg)
lindenmayer.cpp: next word// POST: replaces all symbols in word according to their// production and returns the resultstd::string next_word (std::string word) {
std::string next;for (unsigned int k = 0; k < word.length(); ++k)
next += production (word[k]);return next;
}
// POST: returns the production of cstd::string production (char c) {
switch (c) {case ’F’: return "F+F+";default: return std::string (1, c); // trivial production c −> c}
}
319
![Page 35: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/35.jpg)
lindenmayer.cpp: next word// POST: replaces all symbols in word according to their// production and returns the resultstd::string next_word (std::string word) {
std::string next;for (unsigned int k = 0; k < word.length(); ++k)
next += production (word[k]);return next;
}
// POST: returns the production of cstd::string production (char c) {
switch (c) {case ’F’: return "F+F+";default: return std::string (1, c); // trivial production c −> c}
} 319
![Page 36: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/36.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
320
![Page 37: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/37.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
Springe zum case, der word[k] entspricht.
320
![Page 38: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/38.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
Vorwärts! (Funktion aus unserer Schildkröten-Bibliothek)
320
![Page 39: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/39.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
Überspringe die folgenden cases
320
![Page 40: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/40.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
Drehe dich um 90 Grad! (Funktion aus unsererSchildkröten-Bibliothek)
320
![Page 41: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/41.jpg)
lindenmayer.cpp: draw word// POST: draws the turtle graphic interpretation of wordvoid draw_word (std::string word){
for (unsigned int k = 0; k < word.length(); ++k)switch (word[k]) {case ’F’:
turtle::forward();break;
case ’+’:turtle::left(90);break;
case ’−’:turtle::right(90);
}}
Drehe dich um -90 Grad! (Funktion aus unsererSchildkröten-Bibliothek)
320
![Page 42: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/42.jpg)
L-Systeme: Erweiterungen
Beliebige Symbole ohne grafische Interpretation (dragon.cpp)Beliebige Drehwinkel (snowflake.cpp)Sichern und Wiederherstellen des Schildkröten-Zustandes→Pflanzen (bush.cpp)
321
![Page 43: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/43.jpg)
L-System-Challenge: amazing.cpp!
322
![Page 44: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/44.jpg)
Mehrdimensionale Felder
sind Felder von Felderndienen zum Speichern von Tabellen, Matrizen,...
323
![Page 45: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/45.jpg)
Mehrdimensionale Felder
sind Felder von Feldernint a[2][3]
a hat zwei Elemente, und jedes vonihnen ist ein Feld der Länge 3 mit zu-grundeliegendem Typ int
323
![Page 46: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/46.jpg)
Mehrdimensionale Felder
Im Speicher: flach
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
a[0] a[1]
324
![Page 47: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/47.jpg)
Mehrdimensionale Felder
Im Speicher: flach
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
a[0] a[1]
Im Kopf: Matrix Spalten
Zeilen
0 1 2
0 a[0][0] a[0][1] a[0][2]
1 a[1][0] a[1][1] a[1][2]324
![Page 48: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/48.jpg)
Mehrdimensionale Felder
sind Felder von Feldern von Feldern ....
T a[expr1] ... [exprk]
a hat expr1 Elemente und jedes von ihnen ist ein Feldmit expr2 Elementen, von denen jedes ein Feld mit expr3Elementen ist, ...
Konstante Ausdrücke!
325
![Page 49: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/49.jpg)
Mehrdimensionale Felder
Initialisierung:
int a[2][3] ={
{2,4,6},{1,3,5}}
2 4 6 1 3 5
326
![Page 50: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/50.jpg)
Mehrdimensionale Felder
Initialisierung:
int a[][3] ={
{2,4,6},{1,3,5}}
2 4 6 1 3 5
Erste Dimension kann weggelassen werden
326
![Page 51: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/51.jpg)
Vektoren von Vektoren
Wie bekommen wir mehrdimensionale Felder mit variablenDimensionen?
Lösung: Vektoren von Vektoren
Beispiel: Vektor der Länge n von Vektoren der Länge m:
std::vector<?> a (n, ?);
Zugrundeliegender Typ des ersten Vektors?
327
![Page 52: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/52.jpg)
Vektoren von Vektoren
Wie bekommen wir mehrdimensionale Felder mit variablenDimensionen?Lösung: Vektoren von Vektoren
Beispiel: Vektor der Länge n von Vektoren der Länge m:
std::vector<?> a (n, ?);
Zugrundeliegender Typ des ersten Vektors?
327
![Page 53: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/53.jpg)
Vektoren von Vektoren
Wie bekommen wir mehrdimensionale Felder mit variablenDimensionen?Lösung: Vektoren von Vektoren
Beispiel: Vektor der Länge n von Vektoren der Länge m:
std::vector<std::vector<int> > a (n,std::vector<int>(m));
Zugrundeliegender Typ des ersten Vektors?std::vector<int>, initialisiert mit Länge m:std::vector<int>(m)
327
![Page 54: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/54.jpg)
Anwendung: Kurzeste WegeFabrik-Halle (n×m quadratische Zellen)
S
T
Startposition des RobotersZielposition des Roboters
Hindernis
freie Zelle
Ziel: Finde den kürzestenWeg des Roboters von Snach T über freie Zellen!
328
![Page 55: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/55.jpg)
Anwendung: Kurzeste WegeFabrik-Halle (n×m quadratische Zellen)
S
T
Startposition des RobotersZielposition des Roboters
Hindernis
freie Zelle
Ziel: Finde den kürzestenWeg des Roboters von Snach T über freie Zellen!
328
![Page 56: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/56.jpg)
Anwendung: Kurzeste WegeFabrik-Halle (n×m quadratische Zellen)
S
T
Startposition des RobotersZielposition des Roboters
Hindernis
freie Zelle
Ziel: Finde den kürzestenWeg des Roboters von Snach T über freie Zellen!
328
![Page 57: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/57.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
330
![Page 58: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/58.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
330
![Page 59: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/59.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
Startposition
Zielposition.Kürzester Weg:Länge 21
330
![Page 60: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/60.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
Startposition
Zielposition.Kürzester Weg:Länge 21
21
330
![Page 61: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/61.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
Startposition
Zielposition.Kürzester Weg:Länge 21
21
20
330
![Page 62: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/62.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
Startposition
Zielposition.Kürzester Weg:Länge 21
21
20
19
330
![Page 63: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/63.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
Das löst auch das Original-Problem: Starte in T;folge einem Weg mit sinkenden Längen
Startposition
Zielposition.Kürzester Weg:Länge 21
21
20
19 18
330
![Page 64: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/64.jpg)
Ein (scheinbar) anderes Problem
Finde die Längen der kürzesten Wege zu allen möglichen Zielen
4 5 6 7 8 9 15 16 17 18 19
3 9 10 14 15 16 17 18
2 1 0 10 11 12 13 14 15 16 17
3 2 1 11 12 13 17 18
4 3 2 10 11 12 20 19 18 19
5 4 3 9 10 11 21 20 19 20
6 5 4 8 9 10 22 21 20 21
7 6 5 6 7 8 9 23 22 21 22
331
![Page 65: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/65.jpg)
Vorbereitung: Wachter (Sentinels)
S
T
Zeile 0, Spalte 0 Zeile 0, Spalte m+1
Zeile n, Spalte 0 Zeile n+1, Spalte m+1
Umrandung der Halle mit Sentinels zur Ver-meidung von Spezialfällen
333
![Page 66: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/66.jpg)
Vorbereitung: Initiale Markierung
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-2
start
334
![Page 67: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/67.jpg)
Das Kurzeste-Wege-Programm
// define a two−dimensional array of dimensions// (n+2) x (m+2) to hold the floor// plus extra walls aroundstd::vector<std::vector<int> >
floor (n+2, std::vector<int>(m+2));
// Einlesen der Hallenbelegung, initiale Markierung// (Handout)...// Markierung der umschliessenden Waende (Handout)...
336
![Page 68: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/68.jpg)
Das Kurzeste-Wege-Programm
// define a two−dimensional array of dimensions// (n+2) x (m+2) to hold the floor// plus extra walls aroundstd::vector<std::vector<int> >
floor (n+2, std::vector<int>(m+2));
// Einlesen der Hallenbelegung, initiale Markierung// (Handout)...// Markierung der umschliessenden Waende (Handout)...
Wächter (Sentinel)
336
![Page 69: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/69.jpg)
Markierung aller Zellen mit ihren Weglangen
Schritt 0: Alle Zellen mit Weglänge 0
0
T
Unmarkierte Nachbarn derZellen mit Länge 2
339
![Page 70: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/70.jpg)
Markierung aller Zellen mit ihren Weglangen
Schritt 1: Alle Zellen mit Weglänge 1
1 01
TUnmarkierte Nachbarn derZellen mit Länge 0
Unmarkierte Nachbarn derZellen mit Länge 2
339
![Page 71: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/71.jpg)
Markierung aller Zellen mit ihren Weglangen
Schritt 2: Alle Zellen mit Weglänge 2
2 1 02 1
2
TUnmarkierte Nachbarn derZellen mit Länge 1
Unmarkierte Nachbarn derZellen mit Länge 2
339
![Page 72: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/72.jpg)
Markierung aller Zellen mit ihren Weglangen
Schritt 3: Alle Zellen mit Weglänge 3
32 1 03 2 1
3 23
TUnmarkierte Nachbarn derZellen mit Länge 2
339
![Page 73: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/73.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
340
![Page 74: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/74.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
zeigt an, ob in einem Durchlauf durchalle Zellen Fortschritt gemacht wurde
340
![Page 75: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/75.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
Gehe über alle Zellen
340
![Page 76: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/76.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
Zelle schon markiert oder Hindernis
340
![Page 77: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/77.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
Ein Nachbar hat Weglänge i − 1. DieWächter garantieren immer 4 Nach-barn.
340
![Page 78: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/78.jpg)
Hauptschleife
Finde und markiere alle Zellen mit Weglängen i = 1, 2, 3...for (int i=1;; ++i) {
bool progress = false;for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {if (floor[r][c] != −1) continue;if (floor[r−1][c] == i−1 || floor[r+1][c] == i−1 ||
floor[r][c−1] == i−1 || floor[r][c+1] == i−1 ) {floor[r][c] = i; // label cell with iprogress = true;
}}
if (!progress) break;}
Kein Fortschritt, alle erreichbarenZellen markiert; fertig.
340
![Page 79: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/79.jpg)
Das Kurzeste-Wege-Programm
Algorithmus: Breitensuche
Das Programm kann recht langsam sein, weil für jedes i alleZellen durchlaufen werdenVerbesserung: durchlaufe jeweils nur die Nachbarn der Zellen mitMarkierung i− 1
344
![Page 80: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/80.jpg)
Das Kurzeste-Wege-Programm
Algorithmus: BreitensucheDas Programm kann recht langsam sein, weil für jedes i alleZellen durchlaufen werden
Verbesserung: durchlaufe jeweils nur die Nachbarn der Zellen mitMarkierung i− 1
344
![Page 81: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/81.jpg)
Das Kurzeste-Wege-Programm
Algorithmus: BreitensucheDas Programm kann recht langsam sein, weil für jedes i alleZellen durchlaufen werdenVerbesserung: durchlaufe jeweils nur die Nachbarn der Zellen mitMarkierung i− 1
344
![Page 82: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/82.jpg)
Felder als Funktionsargumente
Felder können auch als Referenz-Argumente an eine Funktionübergeben werden. (Hier const, weil nur Lesezugriff nötig).
void print_vector(const int (&v)[3]) {for (int i = 0; i<3 ; ++i) {
std::cout << v[i] << " ";}
}
345
![Page 83: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/83.jpg)
Felder als Funktionsargumente
Das geht auch für mehrdimensionale Felder.
void print_matrix(const int (&m)[3][3]) {for (int i = 0; i<3 ; ++i) {
print_vector (m[i]);std::cout << "\n";
}}
346
![Page 84: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/84.jpg)
Vektoren als Funktionsargumente
Vektoren können per value, aber auch als Referenz-Argumente aneine Funktion übergeben werden.
void print_vector(const std::vector<int>& v) {for (int i = 0; i<v.size() ; ++i) {
std::cout << v[i] << " ";}
}
Hier: Call by Reference ist effizienter, weil der Vektor sehr lang seinkann.
347
![Page 85: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/85.jpg)
Vektoren als Funktionsargumente
Vektoren können per value, aber auch als Referenz-Argumente aneine Funktion übergeben werden.
void print_vector(const std::vector<int>& v) {for (int i = 0; i<v.size() ; ++i) {
std::cout << v[i] << " ";}
}
Hier: Call by Reference ist effizienter, weil der Vektor sehr lang seinkann.
347
![Page 86: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/86.jpg)
Vektoren als Funktionsargumente
Das geht auch für mehrdimensionale Vektoren.
void print_matrix(const std::vector<std::vector<int> >& m) {for (int i = 0; i<m.size() ; ++i) {
print_vector (m[i]);std::cout << "\n";
}}
348
![Page 87: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/87.jpg)
12. Zeiger, Algorithmen, Iteratoren undContainer I
Zeiger, Address- und Dereferenoperator,Feld-nach-Zeiger-Konversion
349
![Page 88: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/88.jpg)
Komische Dinge. . .#include<iostream>#include<algorithm>
int main(){int a[] = {3, 2, 1, 5, 4, 6, 7};
// gib das kleinste Element in a ausstd::cout << *std::min_element (a, a + 7);
return 0;}
350
![Page 89: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/89.jpg)
Komische Dinge. . .#include<iostream>#include<algorithm>
int main(){int a[] = {3, 2, 1, 5, 4, 6, 7};
// gib das kleinste Element in a ausstd::cout << *std::min_element (a, a + 7);
return 0;}
??? ???
350
![Page 90: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/90.jpg)
Komische Dinge. . .#include<iostream>#include<algorithm>
int main(){int a[] = {3, 2, 1, 5, 4, 6, 7};
// gib das kleinste Element in a ausstd::cout << *std::min_element (a, a + 7);
return 0;}
??? ???
Dafür müssen wir zuerst Zeiger verstehen!350
![Page 91: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/91.jpg)
Referenzen: Wo ist Anakin?
int anakin_skywalker = 9;
int& darth_vader = anakin_skywalker;darth_vader = 22;
// anakin_skywalker = 22
351
![Page 92: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/92.jpg)
Referenzen: Wo ist Anakin? Initialisierung
int anakin_skywalker = 9;int& darth_vader = anakin_skywalker;
darth_vader = 22;
// anakin_skywalker = 22
351
![Page 93: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/93.jpg)
Referenzen: Wo ist Anakin? Zuweisung
int anakin_skywalker = 9;int& darth_vader = anakin_skywalker;darth_vader = 22;
// anakin_skywalker = 22
351
![Page 94: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/94.jpg)
Referenzen: Wo ist Anakin?
int anakin_skywalker = 9;int& darth_vader = anakin_skywalker;darth_vader = 22;
// anakin_skywalker = 22
“Suche nach Vader, undAnakin finden du wirst.”
351
![Page 95: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/95.jpg)
Zeiger: Wo ist Anakin?
int anakin_skywalker = 9;
int* here = &anakin_skywalker;std::cout << here; // Adresse*here = 22;
// anakin_skywalker = 22
“Anakins Adresse ist0x7fff6bdd1b54.”
352
![Page 96: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/96.jpg)
Zeiger: Wo ist Anakin?
int anakin_skywalker = 9;
int* here = &anakin_skywalker;std::cout << here; // Adresse*here = 22;
// anakin_skywalker = 22
“Anakins Adresse ist0x7fff6bdd1b54.”
352
![Page 97: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/97.jpg)
Zeiger: Wo ist Anakin? Initialisierung
int anakin_skywalker = 9;int* here = &anakin_skywalker;
std::cout << here; // Adresse*here = 22;
// anakin_skywalker = 22
“Anakins Adresse ist0x7fff6bdd1b54.”
352
![Page 98: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/98.jpg)
Zeiger: Wo ist Anakin? Ausgabe
int anakin_skywalker = 9;int* here = &anakin_skywalker;std::cout << here; // Adresse
*here = 22;
// anakin_skywalker = 22
“Anakins Adresse ist0x7fff6bdd1b54.”
352
![Page 99: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/99.jpg)
Zeiger: Wo ist Anakin? Zuweisung
int anakin_skywalker = 9;int* here = &anakin_skywalker;std::cout << here; // Adresse*here = 22;
// anakin_skywalker = 22
“Anakins Adresse ist0x7fff6bdd1b54.”
352
![Page 100: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/100.jpg)
Swap mit Zeigern
void swap(int∗ x, int∗ y){int t = ∗x;∗x = ∗y;∗y = t;
}
...int a = 2;int b = 1;swap(&a, &b);std::cout << "a= " << a << "\n"; // 1std::cout << "b = " << b << "\n"; // 2
353
![Page 101: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/101.jpg)
Zeiger Typen
T* Zeiger-Typ zum zugrunde liegendenTyp T.
Ein Ausdruck vom Typ T* heisst Zeiger (auf T).
354
![Page 102: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/102.jpg)
Zeiger Typen
Wert eines Zeigers auf T ist die Adresse eines Objektes vom Typ T.
Beispieleint* p; Variable p ist Zeiger auf ein int.float* q; Variable q ist Zeiger auf ein float.
355
![Page 103: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/103.jpg)
Zeiger Typen
Wert eines Zeigers auf T ist die Adresse eines Objektes vom Typ T.
Ganzzahlwert p = adr
adr
int* p = ...;
355
![Page 104: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/104.jpg)
Adress-Operator
& lval
L-Wert vom Typ T
Typ: T*
Wert: Adresse von lval
356
![Page 105: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/105.jpg)
Adress-Operator
Beispielint i = 5;
int* ip = &i; // ip initialisiert// mit Adresse von i.
i = 5
ip = &i
357
![Page 106: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/106.jpg)
Adress-Operator
Beispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.
i = 5ip = &i
357
![Page 107: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/107.jpg)
Dereferenz-Operator
*rval
R-Wert vom Typ T*
Typ: T
Wert: Wert des Objekts an Addresse rval
358
![Page 108: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/108.jpg)
Dereferenz-OperatorBeispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.int j = *ip; // j == 5
i = 5 ip
j
Wert
359
![Page 109: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/109.jpg)
Dereferenz-OperatorBeispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.int j = *ip; // j == 5
i = 5 ip
j
Wert
359
![Page 110: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/110.jpg)
Dereferenz-OperatorBeispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.int j = *ip; // j == 5
i = 5 ipj
Wert
359
![Page 111: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/111.jpg)
Dereferenz-OperatorBeispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.int j = *ip; // j == 5
*ip = i = 5 ipj
Wert359
![Page 112: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/112.jpg)
Dereferenz-OperatorBeispielint i = 5;int* ip = &i; // ip initialisiert
// mit Adresse von i.int j = *ip; // j == 5
*ip = i = 5 ipj = 5
Wert359
![Page 113: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/113.jpg)
Adress- und Dereferenzoperator
Zeiger (R-Wert)
Objekt (L-Wert)
& *
360
![Page 114: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/114.jpg)
Zeiger-Typen
Man zeigt nicht mit einem double* auf einen int!
Beispiele
int* i = ...; // an Adresse i “wohnt” ein int...double* j = i; //...und an j ein double: Fehler!
361
![Page 115: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/115.jpg)
Zeiger-Typen
Man zeigt nicht mit einem double* auf einen int!
Beispiele
int* i = ...; // an Adresse i “wohnt” ein int...double* j = i; //...und an j ein double: Fehler!
361
![Page 116: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/116.jpg)
Eselsbrucke
Die Deklaration
T* p; p ist vom Typ “Zeiger auf T”
kann gelesen werden als
T *p; *p ist vom Typ T
Obwohl das legal ist,schreiben wir es nicht so!
362
![Page 117: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/117.jpg)
Eselsbrucke
Die Deklaration
T* p; p ist vom Typ “Zeiger auf T”
kann gelesen werden als
T *p; *p ist vom Typ T
Obwohl das legal ist,schreiben wir es nicht so!
362
![Page 118: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/118.jpg)
Eselsbrucke
Die Deklaration
T* p; p ist vom Typ “Zeiger auf T”
kann gelesen werden als
T *p; *p ist vom Typ T
Obwohl das legal ist,schreiben wir es nicht so!
362
![Page 119: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/119.jpg)
Zeiger-Arithmetik: Zeiger plus int
ptr : Zeiger auf a[k] des Arrays a mit Länge n
Wert von expr : ganze Zahl i mit 0 ≤ k + i ≤ n
ptr + expr
ist Zeiger auf a[k + i].
a (a[n])ptr
k
363
![Page 120: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/120.jpg)
Zeiger-Arithmetik: Zeiger plus int
ptr : Zeiger auf a[k] des Arrays a mit Länge nWert von expr : ganze Zahl i mit 0 ≤ k + i ≤ n
ptr + expr
ist Zeiger auf a[k + i].
a (a[n])ptr
k i
363
![Page 121: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/121.jpg)
Zeiger-Arithmetik: Zeiger plus int
ptr : Zeiger auf a[k] des Arrays a mit Länge nWert von expr : ganze Zahl i mit 0 ≤ k + i ≤ n
ptr + expr
ist Zeiger auf a[k + i].a (a[n])ptr
k i
ptr+expr
363
![Page 122: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/122.jpg)
Konversion Feld⇒ Zeiger
Wie bekommen wir einen Zeiger auf das erste Element einesFeldes?
Statisches Feld vom Typ T [n] ist konvertierbar nach T*
Beispielint a[5];int* begin = a; // \trans{begin zeigt auf}{begin points to} a[0]
Längeninformation geht verloren („Felder sind primitiv”).
365
![Page 123: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/123.jpg)
Konversion Feld⇒ Zeiger
Wie bekommen wir einen Zeiger auf das erste Element einesFeldes?
Statisches Feld vom Typ T [n] ist konvertierbar nach T*
Beispielint a[5];int* begin = a; // \trans{begin zeigt auf}{begin points to} a[0]
Längeninformation geht verloren („Felder sind primitiv”).
365
![Page 124: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/124.jpg)
Konversion Feld⇒ Zeiger
Wie bekommen wir einen Zeiger auf das erste Element einesFeldes?
Statisches Feld vom Typ T [n] ist konvertierbar nach T*
Beispielint a[5];int* begin = a; // \trans{begin zeigt auf}{begin points to} a[0]
Längeninformation geht verloren („Felder sind primitiv”).
365
![Page 125: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/125.jpg)
Iteration uber ein Feld mit Zeigern
Beispielint a[5] = {3, 4, 6, 1, 2};for (int* p = a; p < a+5; ++p)
std::cout << *p << ’ ’; // 3 4 6 1 2
a+5 ist ein Zeiger direkt hinter das Ende des Feldes(past-the-end), der nicht dereferenziert werden darf.Zeigervergleich (p < a+5) bezieht sich auf die Reihenfolge derbeiden Adressen im Speicher.
366
![Page 126: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/126.jpg)
Iteration uber ein Feld mit Zeigern
Beispielint a[5] = {3, 4, 6, 1, 2};for (int* p = a; p < a+5; ++p)
std::cout << *p << ’ ’; // 3 4 6 1 2
a+5 ist ein Zeiger direkt hinter das Ende des Feldes(past-the-end), der nicht dereferenziert werden darf.
Zeigervergleich (p < a+5) bezieht sich auf die Reihenfolge derbeiden Adressen im Speicher.
366
![Page 127: 11. Felder (Arrays) II - ETH Z](https://reader031.vdocuments.pub/reader031/viewer/2022012920/61c765ae433541193e79f883/html5/thumbnails/127.jpg)
Iteration uber ein Feld mit Zeigern
Beispielint a[5] = {3, 4, 6, 1, 2};for (int* p = a; p < a+5; ++p)
std::cout << *p << ’ ’; // 3 4 6 1 2
a+5 ist ein Zeiger direkt hinter das Ende des Feldes(past-the-end), der nicht dereferenziert werden darf.Zeigervergleich (p < a+5) bezieht sich auf die Reihenfolge derbeiden Adressen im Speicher.
366