ch06 arrays and vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/cpp_programming/ch06...

30
Photonic Modeling and Design Lab. Graduate Institute of Photonics and Optoelectronics & Department of Electrical Engineering National Taiwan University C++ C++ C++ C++ Programming Programming Programming Programming Chapter 6 Arrays and Vectors Yih Yih Yih Yih-Peng Peng Peng Peng Chiou Chiou Chiou Chiou Room 617, BL Building Room 617, BL Building Room 617, BL Building Room 617, BL Building (02) 3366 (02) 3366 (02) 3366 (02) 3366-3603 3603 3603 3603 [email protected] [email protected] [email protected] [email protected] YPC - NTU GIPO & EE Introduction to C++ Programming NTU BA 2 Objectives Objectives Objectives Objectives This chapter introduces the important topic of data structures—collections of related data items. Arrays are data structures consisting of related data items of the same type. After discussing how arrays are declared, created and initialized, we present a series of practical examples that demonstrate several common array manipulations.

Upload: others

Post on 28-Mar-2021

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

Ph

oto

nic

Mo

de

ling

an

d D

es

ign

La

b.

Gra

du

ate

Ins

titute

of P

ho

ton

ics

an

d O

pto

ele

ctro

nic

s &

De

pa

rtme

nt o

f Ele

ctric

al E

ng

ine

erin

g

Na

tion

al T

aiw

an

Un

ive

rsity

C++C++C++C++Programming

ProgrammingProgrammingProgrammingChapter 6

Arrays and VectorsYihYihYih Yih--- -Peng

PengPengPengChiou

ChiouChiouChiou

Room 617, BL BuildingRoom 617, BL BuildingRoom 617, BL BuildingRoom 617, BL Building

(02) 3366(02) 3366(02) 3366(02) 3366--- -3603

360336033603

[email protected]

[email protected]

[email protected]

[email protected]

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

2

ObjectivesObjectivesObjectivesObjectives

�T

his ch

apter in

trod

uces th

e imp

ortan

t top

ic of d

ata structu

res—co

llections

of related

data item

s.

�A

rrays

are data stru

ctures co

nsistin

g o

f related d

ata items o

f the sam

ety

pe.

�A

fter discu

ssing h

ow

arrays are d

eclared, created

and

initialized

, we

presen

t a series of p

ractical exam

ples th

at dem

on

strate several co

mm

on

array m

anip

ulatio

ns.

Page 2: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

ContentsContentsContentsContents

3

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

con

secutiv

e gro

up

of m

emo

ry lo

cation

s that all h

ave th

e same ty

pe.

�T

o refer to

a particu

lar locatio

n o

r elemen

t in th

e array�

specify

the n

ame

of th

e array an

d th

e po

sition n

um

ber

of th

e elemen

t�

an in

teger array

called c

.

�12 elem

ents.

4

6.2

Arra

ys

�P

ositio

n n

um

ber, fo

rmally

called

a subscrip

tor in

dex

(this n

um

ber sp

ecifies the

num

ber o

f elemen

ts from

the

beg

innin

g o

f the array

).

�1

stelemen

t in ev

ery array

has su

bscrip

t 0 (zero

), and is

som

etimes called

the zero

thelem

ent .

�H

ighest su

bscrip

t in array

cis 1

1 (=

12-1

)

�A

void

off-b

y-o

ne e

rror

�S

pecify

an elem

ent ex

plicitly

�A

subscrip

t must b

e an

integ

er or in

teger ex

pressio

n

(usin

g an

y in

tegral ty

pe).

, e.g., c

[6]

c[6]

c[6]

c[6], c[7]

c[7]

c[7]

c[7]

Page 3: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

5

6.2

Arra

ys

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

rrays o

ccup

y sp

ace in m

emo

ry.

�D

eclaration

form

:

�typearrayName[ arraySize];

�T

he co

mp

iler reserves th

e app

rop

riate amo

un

t of m

emo

ry.

�A

rrays can

be d

eclared to

con

tain v

alues o

f any

non

reference

data ty

pe.

6

6.3

De

cla

ring

Arra

ys

Page 4: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�Initializing an array’s elements to zeros andprinting the array

7

6.4

Ex

am

ple

s U

sin

g A

rray 6.4.1

6.4.16.4.16.4.1Declaring an

Declaring an Declaring an Declaring an

Array and Using a Loop to Initialize the Array’s ElementsArray and Using a Loop to Initialize the Array’s ElementsArray and Using a Loop to Initialize the Array’s ElementsArray and Using a Loop to Initialize the Array’s Elements

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�If th

ere are fewer in

itializers than

elemen

ts in th

e array, th

e remain

ing

array

elemen

ts are initialized

to zero

. (mo

re =>

com

pilatio

n erro

r)

8

6.4

Exam

ple

s U

sin

g A

rray

6.4

.2Initializing an Array in a Declaration with an Initializer List

�If th

e array size is o

mitted

from

a declaratio

n w

ith

an in

itializer list, the co

mp

iler determ

ines th

e n

um

ber o

f elemen

ts in th

e array b

y co

un

ting th

e n

um

ber o

f elemen

ts in th

e initializer list.

Page 5: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�The size of automatic

automaticautomaticautomaticand static

staticstaticstaticarrays should

be a constant. (dynamicdynamicdynamicdynamic=> variable OK)

9

6.4.36.4.36.4.36.4.3Specifying an Array’s Size with a Constant Variable

Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculations

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�L

ine 1

0 u

ses the c

onst

qu

alifierto

declare a so

-called co

nstan

t

variab

learraySize

with

the v

alue 1

0.

�C

on

stant v

ariables m

ust b

e initialized

with

a con

stant ex

pressio

n

wh

en th

ey’re d

eclared an

d can

no

t be m

od

ified th

ereafter.

�C

on

stant v

ariables are also

called n

amed

con

stants

or read

-on

ly

variab

les.

10

6.4.36.4.36.4.36.4.3Specifying an Array’s Size with a Constant Variable

Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculations

Page 6: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

11

6.4.36.4.36.4.36.4.3Specifying an Array’s Size with a Constant Variable

Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable Specifying an Array’s Size with a Constant Variable and Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculationsand Setting Array Elements with Calculations

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

12

Page 7: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

13

6.4

.4Summing the Elements of an Array

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

14

6.4

.5Using Bar Charts to Display Array Data

Graphically

Page 8: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

15

6.4

.5Using Bar Charts to Display Array Data Graphically

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

16

6.4

.6Using the Elements of an Array as Counters

Page 9: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�U

ses arrays to

sum

marize th

e results o

f data co

llected in

a surv

ey.

�F

orty

stud

ents w

ere asked

to rate th

e qu

ality o

f the fo

od

in th

e stud

ent

cafeteria on

a scale of 1

to 1

0 (1

mean

ing

awfu

l and

10

mean

ing

ex

cellent). P

lace the 4

0 resp

on

ses in an

integ

er array an

d su

mm

arize th

e results o

f the p

oll.

�C

++

has n

o a

rray

bo

un

ds ch

eckin

g to

prev

ent th

e com

pu

ter from

referrin

g to

an elem

ent th

at do

es no

t exist.

�T

hu

s, an ex

ecutin

g p

rog

ram can

“walk

off” eith

er end

of an

array

with

ou

t warn

ing

.

�Y

ou

sho

uld

ensu

re that all array

references rem

ain w

ithin

the b

ou

nd

s of

the array

.

17

6.4

.7Using Arrays to Summarize Survey Results

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

18

6.4

.7Using Arrays to Summarize Survey Results

Page 10: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

19

6.4

.7Using Arrays to Summarize Survey Results

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

20

6.4

.7Using Arrays to Summarize Survey Results

Page 11: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

pro

gram

initializes s

tatic

local array

s wh

en th

eir declaratio

ns

are first enco

un

tered.

�If a s

tatic

array is n

ot in

itialized ex

plicitly

by

yo

u, each

elemen

t

of th

at array is in

itialized to

zero b

y th

e com

piler w

hen

the array

is

created.

21

6.4

.8Static Local Arrays and Automatic Local Arrays

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

22

6.4

.8Static Local Arrays and Automatic Local Arrays

Page 12: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

23

6.4

.8Static Local Arrays and Automatic Local Arrays

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

24

6.4

.8Static Local Arrays and Automatic Local Arrays

Page 13: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

25

6.4

.8Static Local Arrays and Automatic Local Arrays

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�T

o p

ass an array

argu

men

t to a fu

nctio

n, sp

ecify th

e nam

e of th

e array

with

ou

t any

brack

ets.�

the array

size is no

rmally

passed

as well

�C

++

passes array

s to fu

nctio

ns b

yreferen

ce�

the called

fun

ction

s can m

od

ify th

e elemen

t in th

e callers’ orig

inal array

s.

�T

he v

alue o

f the n

ame o

f the array

is the ad

dress

in th

e com

pu

ter’s m

emo

ry o

f the first elem

ent o

f the array

.�

the called

fun

ction

kn

ow

s precisely

wh

ere the array

is stored

in m

emo

ry

�In

div

idu

al array elem

ents are p

assed b

y v

alue ex

actly as sim

ple v

ariable

�S

uch

simp

le sing

le pieces o

f data are called

scalarso

r scalar qu

antities.

26

6.5

Pa

ss

ing

Arra

ys

to F

un

ctio

ns

Page 14: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

27

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

28

Page 15: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

29

6.5

Pa

ss

ing

Arra

ys

to F

un

ctio

ns

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

30

6.5

Pa

ss

ing

Arra

ys

to F

un

ctio

ns

Page 16: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

31

6.5

Pa

ss

ing

Arra

ys

to F

un

ctio

ns

(By

structu

re or class)

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

32

6.6

Se

arc

hin

g A

rray

s w

ith L

ine

ar S

ea

rch

Page 17: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

earchin

g: d

etermin

e wh

ether an

array o

f totally

Nelem

ents co

ntain

s

a valu

e that m

atches a certain

key

valu

e.

�L

inear search

com

pares each

elemen

t of an

array w

ith a search

key

�N

o p

articular o

rder: fo

un

d in

the 1

st elemen

t as the last.

�O

n av

erage, co

mp

are the search

key

with

N/2

elemen

ts

33

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�S

ortin

g d

ata

(排序

) -p

lacing

the d

ata into

som

e particu

lar ord

er,

e.g. ascen

din

g o

r descen

din

g

�an

intrig

uin

g p

rob

lem th

at has attracted

som

e of th

e mo

st inten

se

research effo

rts in th

e field o

f com

pu

ter science.

�In

sertion

sort —

a simp

le, bu

t inefficien

t, sortin

g alg

orith

m.

�In

serti-th

elemen

etin

to (i-1

) sorted

elemen

ts to m

ake i

sorted

elem

ents.

�i=

1, n

o so

rting i=

2, i=

3, …

34

6.7

So

rting

Arra

ys

with

Ins

ertio

n S

ort

Page 18: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

35

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

36

Page 19: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�A

rrays w

ith tw

o d

imen

sion

s (i.e., sub

scripts) o

ften rep

resent tab

les o

f valu

es con

sisting

of in

form

ation

arrang

ed in

row

san

d co

lum

ns.

�T

o id

entify

a particu

lar table elem

ent w

ith tw

o su

bscrip

ts.

�B

y co

nv

entio

n, th

e 1st=

> ro

w th

e 2n

d=

> co

lum

n.

�O

ften called

two

-dim

ensio

nal array

so

r 2-D

arrays.

�A

rrays w

ith tw

o o

r mo

re dim

ensio

ns are k

no

wn

as mu

ltidim

ensio

nal

arrays.

37

6.8

Mu

ltidim

en

sio

na

l Arra

ys

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

an b

e initialized

in d

eclaration

, valu

es gro

up

ed b

y ro

w in

braces

�If n

ot en

ou

gh

initializers fo

r a giv

en ro

w, rem

ainin

g elem

ents o

f that

row

are initialized

to 0

.

38

6.8

Mu

ltidim

en

sio

na

l Arra

ys

Page 20: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

39

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

40

6.9

Case S

tud

y: P

rocessin

g G

rad

es in

a

Tw

o-D

imen

sio

nal A

rray

Page 21: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

41

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

42

Page 22: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

43

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

44

Page 23: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

45

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�vector

: a mo

re rob

ust ty

pe o

f array featu

ring

man

y ad

ditio

nal cap

abilities

�C

-style p

oin

ter-based

arrays: p

oten

tial for erro

rs and

are no

t flexib

le

�can

easily “w

alk o

ff” either en

d o

f an array

, becau

se C+

+ d

oes n

ot ch

eck

�T

wo

arrays can

no

t be m

eanin

gfu

lly co

mp

ared w

ith relatio

nal o

perato

rs

�W

hen

an array

is passed

to a g

eneral-p

urp

ose fu

nctio

n d

esign

ed to

han

dle

arrays o

f any size, its size m

ust b

e passed

as an ad

ditio

nal arg

um

ent.

�O

ne array

cann

ot b

e assign

ed to

ano

ther w

ith th

e assign

men

t op

erator(s).

�C

lass temp

late vector

allow

s yo

u to

create a mo

re po

werfu

l and

less erro

r-pro

ne altern

ative to

arrays.

�S

tand

ard class tem

plate v

ector

is defin

ed in

head

er <vector>

and

belo

ng

s to

nam

espace s

td

46

6.1

0In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

Tem

pla

te vector

vector

vector

vector

Page 24: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

47

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

48

Page 25: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

49

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

50

Page 26: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

51

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�B

y d

efault, all th

e elemen

ts of a v

ector

ob

ject are set to 0

.

�vector

s can b

e defin

ed to

store an

y d

ata typ

e.

�vector

mem

ber fu

nctio

n size

ob

tain th

e nu

mb

er of elem

ents.

�U

can u

se squ

are brack

ets, []

, to access th

e elemen

ts in a v

ector

.

�vector

ob

jects can b

e com

pared

with

on

e ano

ther u

sing

the

equ

ality o

perato

rs.

�Y

ou

can create a n

ew vector

ob

ject that is in

itialized w

ith a co

py

o

f an ex

isting

vector

.

�Y

ou

can u

se the assig

nm

ent (=

) op

erator w

ith vector

ob

jects.

�A

s with

C-sty

le po

inter-b

ased array

s, C+

+ d

oes n

ot p

erform

any

bo

un

ds ch

eckin

g w

hen

vector

elemen

ts are accessed w

ith sq

uare

brack

ets.

�S

tand

ard class tem

plate v

ector

pro

vid

es bo

un

ds ch

eckin

g in

its

mem

ber fu

nctio

n at

, wh

ich “th

row

s an ex

ceptio

n” (see C

hap

ter1

6,

Ex

ceptio

n H

and

ling

) if its argu

men

t is an in

valid

sub

script.

52

6.1

0In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

Tem

pla

te vector

vector

vector

vector

Page 27: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

53

6.1

1In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

string

string

string

string

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

54

Page 28: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

55

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

56

Page 29: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�L

ine 1

6 read

s the lin

e of tex

t from

the u

ser by

usin

g th

e library

fun

ction

getline

(from

the h

eader file <

string>

).

�We can’t simply write �

cin>> s4; to

ob

tain a lin

e of tex

t.

�W

hen

cinis u

sed w

ith th

e stream ex

traction

op

erator, it read

s

characters u

ntil th

e first wh

ite-space ch

aracter is reached

.

�L

ines 2

7–

33

sho

w th

e results o

f com

parin

g strin

gs b

y u

sing

class

string

’s ov

erload

ed eq

uality

and

relation

al op

erators.

�T

hese o

perato

rs perfo

rm lex

icog

raph

ical com

pariso

ns o

n strin

g

ob

jects—th

ey co

mp

are the n

um

erical valu

es of th

e characters

(see Ap

pen

dix

B, A

SC

II Ch

aracter Set) in

each strin

g.

�C

lass string

pro

vid

es mem

ber fu

nctio

n em

pty

, wh

ich retu

rns tru

e if

the strin

g is em

pty

; oth

erwise, it retu

rns false.

57

6.1

1In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

string

string

string

string

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�L

ine 4

1 d

emo

nstrates class x

’s ov

erload

ed assig

nm

ent o

perato

r.

�L

ine 4

7 d

emo

nstrates class strin

g’s o

verlo

aded

+=o

perato

r for

string

con

catenatio

n.

�L

ine 5

2 d

emo

nstrates th

at a string

literal can also

be ap

pen

ded

to

a string

ob

ject by

usin

g o

perato

r +=.

�C

lass string

pro

vid

es mem

ber fu

nctio

n substr

(lines 5

8 an

d 6

3) to

return

a po

rtion

of a strin

g as a strin

g o

bject.

�T

he v

ersion

with

two

argu

men

ts ob

tains th

e nu

mb

er of

characters sp

ecified b

y th

e secon

d arg

um

ent startin

g at th

e

po

sition

specified

by

the first arg

um

ent.

�T

he sin

gle arg

um

ent v

ersion

ob

tains a su

bstrin

g startin

g fro

m th

e

specified

ind

ex.

�C

lass string

’s ov

erload

ed []

op

erator can

be u

sed to

create lvalu

es

that en

able n

ew ch

aracters to rep

lace existin

g ch

aracters in a strin

g.

�N

o b

ou

nd

s check

ing

is perfo

rmed

.

58

6.1

1In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

string

string

string

string

Page 30: Ch06 Arrays and Vectors - 國立臺灣大學ccf.ee.ntu.edu.tw/~ypchiou/Cpp_Programming/Ch06 Arrays... · 2012. 5. 16. · Chapter 6 Arrays and Vectors Yih YYiihh Yih----Peng Peng

YP

C -

NT

U G

IPO

& E

EIn

trod

uctio

n to

C+

+ P

rog

ram

min

gN

TU

BA

�C

lassstring’s o

verlo

aded

[]

op

erator can

be u

sed to

ob

tain th

e

character at a sp

ecified in

dex

.

�C

lassstring

pro

vid

es bo

un

ds ch

eckin

g in

its mem

ber fu

nctio

n

at, w

hich

thro

ws an

excep

tion

if its argu

men

t is an in

valid

sub

script.

�Erase a vector : http://www.cplusplus.com/reference/stl/vector/erase/

�Two and multi-dimensional vectors

59

6.1

1In

trod

uctio

n to

C+

+ S

tan

dard

Lib

rary

Cla

ss

string

string

string

string