CS253: Software Development with C++

Spring 2020

IQ 11

CS253 IQ 11

Show Main.IQ11 as a slide show.

Which of these is a valid declaration?

  1. template <int I, typename T = long double>
    class Foo { };
  2. template <class C, long L, bool B = false>
    class Foo { };
  3. template <int *IP> class Foo { };
  4. All of the above
  5. template <double D> class Foo { };

Everything but double D. You can pass double as a type, but not as a value.

Front/back

What will this code emit?

vector<int> v = {2,3,5,7};
for (auto it = v.front(); it != v.back(); ++it)
    cout << it;
23456
  1. 235
  2. 2357
  3. 23456
  4. 234567
  5. None of the above

.front() and .back() return references to values, not iterators.

Subtraction

What will this code emit?

vector<int> v = {1,2,3};
cout << v.end() - v.begin() << endl;
3
  1. 123
  2. 2
  3. 3
  4. Depends on the size of an int.
  5. It will not compile.

v.end() “points” one past the last element, not to last element.

Traversal #1

What will this code emit?

set<int> s = {2,4,1,3};
for (auto it = s.begin(); it < s.end(); ++it)
    cout << *it;
c.cc:2: error: no match for 'operator<' in 'it < s.std::set<int>::end()' 
   (operand types are 'std::_Rb_tree_const_iterator<int>' and 
   'std::set<int>::iterator' {aka 'std::_Rb_tree_const_iterator<int>'})
  1. 2413
  2. 1234
  3. 123
  4. It depends on where the tree nodes are allocated.
  5. None of the above.

You can’t use < on a set::iterator

Traversal #2

What will this code emit?

set<int> s = {2,4,1,3};
for (auto it = s.begin(); it != s.end(); ++it)
    cout << *it;
1234
  1. 2413
  2. 1234
  3. 123
  4. It depends on where the tree nodes are allocated.
  5. None of the above.

Sets are sorted.