Chapter 14: Standard C++ Vectors

Example 14.1, Page no: 324

In [1]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
print_(v)
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya

Example 14.2, Page no: 325

In [2]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
print_(v)
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya

Example 14.3, Page no: 326

In [3]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in range(len(v)):
        print v[i]

v = []
load(v)
print_(v)
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya

Example 14.4, Page no: 326

In [4]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
v.sort()
print_(v)
Chile
Egypt
Italy
Japan
Kenya
Nepal
Spain
Zaire

Example 14.5, Page no: 327

In [5]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
w = v
print_(v)
print_(w)
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya

Example 14.6, Page no: 327

In [6]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
v.sort()
print_(v)
print "v.front() = " + v[0]
print "v.back() = " + v.pop(-1) 
print "v.back() = " + v.pop(-1) 
print "v.back() = " + v[-1] 
print_(v)
Chile
Egypt
Italy
Japan
Kenya
Nepal
Spain
Zaire
v.front() = Chile
v.back() = Zaire
v.back() = Spain
v.back() = Nepal
Chile
Egypt
Italy
Japan
Kenya
Nepal

Example 14.7, Page no: 328

In [7]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i

v = []
load(v)
v.sort()
print_(v)
v.pop(2) # removes Italy
v.pop(-2) # removes Spain
print_(v)
Chile
Egypt
Italy
Japan
Kenya
Nepal
Spain
Zaire
Chile
Egypt
Japan
Kenya
Nepal
Zaire

Example 14.8, Page no: 329

In [8]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")

def print_(v):
    for i in v:
        print i
v = []
load(v)
v.sort()
print_(v)
r = []
for i in range(2,len(v)-2):
    r.append(v[i]) #removes the segment Italy..Nepal
    
for i in r:
    v.remove(i)
print_(v)
v.insert(2,"India")
print_(v)
Chile
Egypt
Italy
Japan
Kenya
Nepal
Spain
Zaire
Chile
Egypt
Spain
Zaire
Chile
Egypt
India
Spain
Zaire

Example 14.9, Page no: 329

In [9]:
def load(v):
    v.append("Japan")
    v.append("Italy")
    v.append("Spain")
    v.append("Egypt")
    v.append("Chile")
    v.append("Zaire")
    v.append("Nepal")
    v.append("Kenya")
    v.append("India")
    v.append("China")
    v.append("Malta")
    v.append("Syria")

def print_(v):
    for i in v:
        print i

v = []
load(v)
print_(v)
egypt = v.index('Egypt')
malta = v.index('Malta')
w = v[egypt:malta+1]
w.sort()
print_(w)
Japan
Italy
Spain
Egypt
Chile
Zaire
Nepal
Kenya
India
China
Malta
Syria
Chile
China
Egypt
India
Kenya
Malta
Nepal
Zaire

Example 14.10, Page no: 329

In [10]:
def copy(v,x,n):
    for i in x:
        v.append(i)

def projection(v,b):
    v_size = len(v)
    w = []
    for i in range(0,v_size):
        if b[i]:
            w.append(v[i])
    return w

def print_(v):
    for i in v:
        print i,
    print ''

x = [ 22.2, 33.3, 44.4, 55.5, 66.6, 77.7, 88.8, 99.9 ]
v = []
copy(v, x, 8)
y = [ False, True, False, True, True, True, False, True ]
b = []
copy(b, y, 8)
w = projection(v, b)
print_(v)
print_(w)
22.2 33.3 44.4 55.5 66.6 77.7 88.8 99.9 
33.3 55.5 66.6 77.7 99.9