CS3: PROGRAMMING PATTERNS EXAM #1 name: first ________________ last _____________________ The number of points for each question is given in square brackets. 1. [10] Name and differentiate the following two constructs: for(auto e: myContainer) and for(auto &e: myContainer) Explain the situation where one construct is preferred to the other. Give an example usage. 2. [10] Explain the differences between shallow copy and deep copy. Explain the kind of classes where the two copies differ. State the language constructs that eliminate problems that this difference creates. 3. [10] Define templates and explain their purpose. Differentiate standalone function and class templates. Give an example of a class template and a function template. 4. [15] Differentiate sequential, associative and unordered STL containers. Explain the need for specialized list algorithms, i.e. why it is not sufficient to use general purpose STL algorithms. Give an example use of one such algorithm. 5. [10] Define container adapters that we studied. Explain why container adapters more suitable for some tasks than regular sequential container. State one container adapter and describe operations that are available for this adapter. 6. [15] Define iterator invalidation. Explain the reason for this invalidation. Point out the problem in the following code and correct it. vector v = {10, 20, 30, 40, 50, 60}; int i; cout << "Enter number to remove from vector: "; cin >> i; for(auto it = v.begin(); it != v.end(); ++it) if(*it == i) v.erase(it); 7. [15] Define a callback. Name three kinds of callbacks that we studied. Simplify the following code using a lambda-expression. bool notZero(int e){return e!=0;} ... auto it=find_if(vect.begin(), vect.end(), notZero); 8. [15] Explain why modifying STL algorithms such as remove() and unique() do not actually eliminate the unsuitable elements from the container. Describe remove-erase idiom. Assuming that "v" is a vector of integers. Explain what the below code does. v.erase(remove_if(v.begin(), v.end(), [](int i){return i > 5;}), v.end());