Professional Documents
Culture Documents
Array & Vector 2
Array & Vector 2
Array & Vector 2
Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John
Wiley & Sons
Objectives
2
Common Array and Vector
Algorithms
double total = 0;
for (int i = 0; i < data.size(); i++)
{
total = total + data[i];
}
1 | 4 | 9 | 16 | 25
9
Common Algorithms – Element
Separators
Print the separator before each element
except the initial one (with index 0):
10
Common Algorithms – Counting
Matches
How many elements of a vector fulfill a particular criterion?
11
Common Algorithms – Counting
Matches
Which elements fulfill a criterion?
Use a second vector to collect the matches.
Here we collect all elements that are greater than 100.
vector<double> matches;
for (int i = 0; i < data.size(); i++)
{
if (data[i] > 100)
{
matches.push_back(data[i]);
}
}
12
Common Algorithms – Removing
an Element, Unordered
Suppose you want to remove the element at index i.
If the elements in the vector are not in any particular order, that
task is easy to accomplish.
13
Illustration of Removing
Element - Unordered
Let’s say we want to remove item at index 3
0
0 01 1 12 2 23 3 34 4 45 5 56 6 67 7 78 8 89 9
10
10 1020
20 2030
30 3040 50
50 5060
100 100 60 6070
70 7080
80 8090
90 90100
100
Common Algorithms – Removing
an Element, Ordered
The situation is more complex if the order of the elements
matters.
Then you must move all elements following the element to
be removed “down” (to a lower index), and then shrink the
size of the vector by removing the last element.
15
Common Algorithms – Removing
an Element, Ordered
for (int i = pos; i < data.size() - 1; i++)
{
data[i] = data[i + 1];
}
data.pop_back();
16
Illustration of Removing Element -
Ordered
Let’s say we want to remove item at index 6
for (int i = pos; i < data.size() - 1; i++)
data[i] = data[i + 1];
data.pop_back();
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
10
0 20
1 30
2 40
3 50
4 60
5 80
6
70 80
7 90
8 100
9
10 20 30 40 50 60 80 90 90 100
0
10 1
20 2
30 3
40 4
50 5
60 6
80 7
90 8
100 100
10 20 30 40 50 60 80 90 100
Common Algorithms – Inserting
an Element Unordered
18
Common Algorithms –
Inserting an Element Ordered
If the order of the elements
does matter, it is a bit harder.
To insert an element at position i,
all elements from that location to the
end of the vector must be moved
“down”.
After that, insert the new element
at the now vacant position [i].
19
Common Algorithms – Inserting
an Element Ordered
To being this process, add a new element at the “top”
of the vector by copying the last element to that new position.
Then do the shift.
int last_pos = data.size() - 1;
// shift
for (int i = last_pos; i > pos; i--)
data[i] = data[i - 1];
// insert
data[pos] = new_element;
20
Illustration of Inserting Element -
Ordered
Let’s say we want to insert new item (110) at index 6
int last_pos = data.size() - 1; //set last_pos to final index
data.push_back(data[last_pos]); /*make room for new element by
copying the element of last
element */
for (int i = last_pos; i > pos; i--)
data[i] = data[i - 1];
data[pos] = new_element; //set last_pos to final index
00
0 11
1
1 22
2
2 33
3
3 44
4
4 55
5
5 66
6
6 77 7 88 8 99 9 10
7 8 9
10
10
0
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10
10
10 20
20
20 30
3030 40
40 40 5050
50 6060
60 70
70 70 80
80 80 90
90 90100
90 100
100
100
10
10 20
20 30
30 40
40 50
50 60
60 70
70 80 80 90 100
10 20 30 40 50 60 110 70
70 80 80 9090 100
100
Common Algorithms – Maximum
me?
22
Common Algorithms – Maximum
double input;
while (cin >> input){
data.push_back(input);
}
double largest = data[0];
for (int i = 1; i < data.size(); i++)
{
if (data[i] > largest)
{ largest = data[i];}
}
23
Common Algorithms – Maximum
// display, indicating largest
for (int i = 0; i < data.size(); i++)
{
cout << data[i];
if (data[i] == largest) {
cout << " <== largest value";}
cout << endl;}
return 0;
}
24
Vectors and Arrays in Functions
You know that
functions
25
Vectors Parameters – Without
Changing the Values
For example, the following function computes
the sum of a vector of floating-point numbers:
double sum(vector<double> data)
{
double total = 0;
for (int i = 0; i < data.size(); i++)
{
total = total + data[i];
}
return total;
}
This function visits the vector elements,
but it does not change them.
26
Vectors Parameters – Changing
the Values
Sometimes the function should change
the values stored in the vector:
27
Vectors Parameters – Changing
the Values
Sometimes the function should change
the values stored in the vector:
??? squares(int n)
{
int result[]
for (int i = 0; i < n; i++)
{
result[i] = i * i;
}
return result; // ERROR
}
32
Arrays as Parameters in Functions
The caller must provide an array to be used:
33
SUMMARY
❑ Common algorithms for vector:
❑ Find maximum or minimum value
❑ Insert or remove value
❑ Update values
❑ Searching and matching
❑ Vectors can occur as function parameters and return
values.
❑ Array parameters are always passed by reference.
❑ A function’s return type cannot be an array.
34