Professional Documents
Culture Documents
Standard
Template
Library (STL)
Theodore Roethke
OBJECTIVES
In this chapter you will learn:
William Shakespeare
Augustine Birrell
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Self-Review Exercises
State whether the following are true or false or fill in the blanks. If the answer is false, explain why,.
23.1
23.2
23.3
23.4
23.5
23.6
(T/F) The STL makes abundant use of inheritance and virtual functions.
ANS: False. These were avoided for performance reasons.
containers.
ANS: Associative.
,
,
The five main iterator types are
.
ANS: Input, output, forward, bidirectional, random access.
and
ANS: True.
ANS: True.
(T/F) STL algorithms are encapsulated as member functions within each container class.
ANS: False. STL algorithms are not member functions. They operate indirectly on contain-
23.7 (T/F) The remove algorithm does not decrease the size of the vector from which elements
are being removed.
ANS: True.
23.8
and
23.9
(T/F) Container member function end yields the position of the last element of the container.
ANS: False. It actually yields the position just after the end of the container.
iterator.
Exercises
23.12 Write a function template palindrome that takes a vector parameter and returns true or
false according to whether the vector does or does not read the same forward as backward (e.g., a
vector containing 1, 2, 3, 2, 1 is a palindrome, but a vector containing 1, 2, 3, 4 is not).
ANS:
1
2
3
4
5
6
7
8
9
10
11
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
75 74 73 72 71 70 69 68 67 66 65 is not a palindrome
K J I H G F G H I J K is a palindrome
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
23.13 Modify Fig. 23.40, the Sieve of Eratosthenes, so that, if the number the user inputs into the
program is not prime, the program displays the prime factors of the number. Remember that a
prime numbers factors are only 1 and the prime number itself. Every nonprime number has a
unique prime factorization. For example, the factors of 54 are 2, 3, 3 and 3. When these values are
multiplied together, the result is 54. For the number 54, the prime factors output should be 2 and 3.
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
29
79
139
199
271
349
421
491
577
647
733
821
887
983
31
83
149
211
277
353
431
499
587
653
739
823
907
991
37
89
151
223
281
359
433
503
593
659
743
827
911
997
23.14 Modify Exercise 23.13 so that, if the number the user inputs into the program is not prime,
the program displays the prime factors of the number and the number of times each prime factor
appears in the unique prime factorization. For example, the output for the number 54 should be
The unique prime factorization of 54 is: 2 * 3 * 3 * 3
ANS:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
Exercises
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
int counter;
std::bitset< SIZE > sieve; // create bitset of 1024 bits
sieve.flip(); // flip all bits in sieve
sieve.reset( 0 ); // reset first bit (number 0)
sieve.reset( 1 ); // reset second bit (number 1)
// perform Sieve of Eratosthenes
int finalBit = sqrt( static_cast< double > ( sieve.size() ) ) + 1;
for ( i = 2; i < finalBit; i++ )
{
if ( sieve.test( i ) )
{
for ( int j = 2 * i; j < SIZE; j += i )
sieve.reset( j );
} // end if
} // end for
cout << "The prime numbers in the range 2 to 1023 are:\n";
// display prime numbers in range 2-1023
for ( i = 2, counter = 1; i < SIZE; i++ )
{
if ( sieve.test( i ) )
{
cout << setw( 5 ) << i;
if ( counter++ % 12 == 0 )
cout << '\n';
} // end if
} // end for
cout << endl;
// get a value from the user to determine if it is prime
cout << "\nEnter a value from 1 to 1023 (-1 to end): ";
cin >> value;
while ( value != -1 )
{
if ( sieve[ value ] )
cout << value << " is a prime number\n";
else
{
cout << value << " is not a prime number\n"
<< "The unique prime factorization of " << value << " is: ";
for ( int f = 2; f < SIZE; )
{
if ( sieve.test( f ) && value % f == 0 )
{
cout << f; // output factor
value /= f; // modify value
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.
8
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
29
79
139
199
271
349
421
491
577
647
733
821
887
983
31
83
149
211
277
353
431
499
587
653
739
823
907
991
37
89
151
223
281
359
433
503
593
659
743
827
911
997
2006 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved.