Chapter 6: Templates, Iterators, STL

0.0(0)
studied byStudied by 1 person
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/12

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

13 Terms

1
New cards
  1. What is the primary purpose of template functions?

    • A. To allow a single function to be used with varying types of arguments

    • B. To hide the name of the function from the linker (preventing duplicate symbols)

    • C. To implement container classes

    • D. To permit the use of the debugger without the -gstabs flag

A. To allow a single function to be used with varying types of arguments

2
New cards
  1. Consider this prototype for a template function:

    
        template <class Item>
        void foo(Item x);
    

    Which is the right way to call the foo function with an integer argument i?

    • A. foo( i );

    • B. foo<int>( i );

    • C. foo<Item>( i );

    • D. foo(<int> i );

    • E. foo(<Item> i );

Answer: A. foo(i);

3
New cards
  1. Consider the following definition:

    
        template <class Item>
        Item maximal (Item a, Item b)
        {
            if (a > b)
                return a;
            else
                return b;
        }
    

    What restrictions are placed on the Item data type for a program that uses the maximal function?

    • A. The Item data type must be either int, double, or float.

    • B. The Item data type must be one of the built-in C++ data types.

    • C. The Item data type must have a copy constructor and a > operator defined.

    • D. None of the above restrictions apply.

C. The Item data type must have a copy constructor and a > operator defined.

4
New cards
  1. When should a function be implemented as a template function?

    • A. When the data types of the parameters all have copy constructors.

    • B. When the function depends on an underlying data type.

    • C. When the function is relatively short (usually just one line).

    • D. When the function only takes one argument.

B. When the function depends on an underlying data type.

5
New cards
  1. What is a major difference between the header file for a toolkit of template functions and the header file for a toolkit of ordinary functions?

    • A. The ordinary function toolkit header file must have a #include for the implementation file, but the template version does not have this #include.

    • B. The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include.

    • C. The ordinary function toolkit header file must have a macro guard, but the template version does not have this macro guard.

    • D. The template function toolkit header file must have a macro guard, but the ordinary version does not have this macro guard.

B. The template function toolkit header file must have a #include for the implementation file, but the ordinary version does not have this #include.

6
New cards

Why is it a bad idea to have a size_t parameter for a template function?

  • A. Because it would require stdlib.h to be separately compiled.

  • B. Because most compilers would not permit the actual argument to be 42 (an int).

  • C. Because size_t values cannot be negative.

  • D. Because the compiler can tell the size of something without having a size_t parameter.

C. Because size_t values cannot be negative.

7
New cards
  1. Our original bag classes in Chapters 3-5 used a typedef to define the Item data type. What problem is solved by using a template bag class instead of these original typedef versions?

    • A. None of the typedef versions permit a program to use a bag of Strings.

    • B. With all of the typedef versions, it is difficult for a program to use several bags with different Item types.

    • C. With all of the typedef versions, the CAPACITY of the bag is fixed during compilation. A program cannot dynamically allocate a bag.

    • D. With all of the typedef versions, the Item data type must be one of the built-in C++ data types (char, int, etc.)

B. With all the typedef versions, it is difficult for a program to use several bags with different Item types.

8
New cards
  1. Suppose bag is a template class, what is the syntax for declaring a bag b of integers?

    • A. bag b;

    • B. bag<int> b;

    • C. bag of int b;

    • D. int bag b;

B. bag<int> b;

9
New cards
  1. Why is it recommended to first implement a class using a typedef for the Item type, before implementing the template version?

    • A. It is easier to debug the typedef version.

    • B. The typedef version requires less disk space during the design stage.

    • C. The typedef version requires less memory during execution.

    • D. The template version will not compile unless you implement the typedef version first.

A. It is easier to debug the typedef version.

10
New cards

When you write a template class, where does the template prefix occur?

  • A. Before the template class definition

  • B. Before each member function implementation.

  • C. Before any other template functions that manipulate the template class.

  • D. TWO of the above answers are correct.

  • E. All of the (A), (B), and (C) are correct.

  • D. TWO of the above answers are correct.

11
New cards
  1. Suppose that a program wants to use both a bag of doubles and a bag of strings. Which version of the bag could you use?

    • A. You can use the array version of the non-template bag

    • B. You can use the linked list version of the non-template bag

    • C. You can use the array version of the template bag

    • D. Two of the above answers are right

    • E. Answers A, B, and C are all right

C. You can use the array version of the template bag.

12
New cards
  1. What technique is used to provide the capability to step through items of a container class?

    • A. A copy constructor.

    • B. A default constructor.

    • C. A destructor.

    • D. An iterator.

    • E. An overloaded assignment operator.

D. An iterator.

13
New cards
  1. Why does the new node template class require two versions of the data member function, but the node in Chapter 5 needed only one?

    • A. The Chapter 5 node was singly linked, but the node template class is doubly linked.

    • B. The Chapter 5 data function returned a copy of the data, but the data function for the node template class returns a reference to the data.

    • C. The Chapter 5 node had no iterator.

    • D. All of the above.

B. The Chapter 5 data function returned a copy of the data, but the data function for the node template class returns a reference to the data.