|
| 1 | +\model{List and Pair} |
| 2 | + \begin{center} |
| 3 | + \small |
| 4 | + \begin{tabular}{p{3.0in} p{2.8in}} |
| 5 | + \begin{minipage}{3.0in} |
| 6 | + \begin{cpplst} |
| 7 | +#include <list> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +list<string> tasks = |
| 11 | + {"breakfast", "class", "lunch"}; |
| 12 | + |
| 13 | +tasks.push_front("wake up"); |
| 14 | +tasks.push_back("dinner"); |
| 15 | +tasks.pop_front(); |
| 16 | + |
| 17 | +for (string task : tasks) { |
| 18 | + cout << task << endl; |
| 19 | +} |
| 20 | + \end{cpplst} |
| 21 | + \end{minipage} |
| 22 | + & |
| 23 | + \begin{minipage}{2.8in} |
| 24 | + \vskip 10pt |
| 25 | + {\bf After push\_front("wake up"):}\\[4pt] |
| 26 | + \begin{tabular}{|c|c|c|c|c|} |
| 27 | + \hline |
| 28 | + wake up & breakfast & class & lunch \\ |
| 29 | + \hline |
| 30 | + \end{tabular}\\[8pt] |
| 31 | + {\bf After push\_back("dinner"):}\\[4pt] |
| 32 | + \begin{tabular}{|c|c|c|c|c|} |
| 33 | + \hline |
| 34 | + wake up & breakfast & class & lunch & dinner \\ |
| 35 | + \hline |
| 36 | + \end{tabular}\\[8pt] |
| 37 | + {\bf After pop\_front():}\\[4pt] |
| 38 | + \begin{tabular}{|c|c|c|c|} |
| 39 | + \hline |
| 40 | + breakfast & class & lunch & dinner \\ |
| 41 | + \hline |
| 42 | + \end{tabular}\\[10pt] |
| 43 | + {\bf Output:} |
| 44 | + \hrule\vskip 5pt |
| 45 | + {\tt breakfast}\\ |
| 46 | + {\tt class}\\ |
| 47 | + {\tt lunch}\\ |
| 48 | + {\tt dinner} |
| 49 | + \end{minipage} |
| 50 | + \end{tabular} |
| 51 | + \vskip 12pt |
| 52 | + \renewcommand{\arraystretch}{1.3} |
| 53 | + \begin{tabular}{|c|c|c|} |
| 54 | + \hline |
| 55 | + \rowcolor{orange!20} |
| 56 | + \textbf{Operation} & \textbf{vector} & \textbf{list} \\ |
| 57 | + \hline |
| 58 | + \cpp{push_back(val)} & yes & yes \\ |
| 59 | + \hline |
| 60 | + \cpp{pop_back()} & yes & yes \\ |
| 61 | + \hline |
| 62 | + \cpp{push_front(val)} & no & yes \\ |
| 63 | + \hline |
| 64 | + \cpp{pop_front()} & no & yes \\ |
| 65 | + \hline |
| 66 | + \cpp{at(i)} & yes & no \\ |
| 67 | + \hline |
| 68 | + range-based for & yes & yes \\ |
| 69 | + \hline |
| 70 | + \end{tabular} |
| 71 | + \vskip 12pt |
| 72 | + \begin{minipage}{3.5in} |
| 73 | + \begin{cpplst} |
| 74 | +pair<string, int> student = {"Alice", 95}; |
| 75 | +cout << student.first << " scored "; |
| 76 | +cout << student.second << endl; |
| 77 | + \end{cpplst} |
| 78 | + \end{minipage} |
| 79 | + \quad |
| 80 | + \begin{minipage}{2in} |
| 81 | + {\bf Output:} |
| 82 | + \hrule\vskip 5pt |
| 83 | + {\tt Alice scored 95} |
| 84 | + \end{minipage} |
| 85 | + \end{center} |
| 86 | + |
| 87 | + {\it\large Refer to Model 2 above as your team develops consensus answers |
| 88 | + to the questions below.} |
| 89 | + |
| 90 | + \quest{15 min} |
| 91 | + |
| 92 | + \Q What {\tt \#include} header is required to use each of the following? |
| 93 | + \begin{enumerate} |
| 94 | + \itemsep 10pt |
| 95 | + \item \cpp{list} \hfill\ans[2.5in]{\cpp{#include <list>}} |
| 96 | + \item \cpp{pair} \hfill\ans[2.5in]{\cpp{#include <utility>}} |
| 97 | + \end{enumerate} |
| 98 | + |
| 99 | + \vskip -20pt |
| 100 | + |
| 101 | + \Q Using the operations table in the model, answer the following. |
| 102 | + \begin{enumerate} |
| 103 | + \itemsep 10pt |
| 104 | + \item Write a line of C++ to access the 3rd element (index~2) |
| 105 | + of a {\tt vector<int>} named {\tt v}. |
| 106 | + \hfill\ans[2in]{\cpp{v.at(2)}} |
| 107 | + |
| 108 | + \item Can you do the same operation with a {\tt list}? |
| 109 | + Explain why or why not. |
| 110 | + \begin{answer}[0.5in] |
| 111 | + No. A \cpp{list} does not support \cpp{at(i)} because it is |
| 112 | + a linked structure; elements are not stored contiguously, |
| 113 | + so random access by index is not supported. |
| 114 | + \end{answer} |
| 115 | + |
| 116 | + \item A {\tt vector} does not support {\tt push\_front}. Why |
| 117 | + might adding an element to the front of a vector be slow compared |
| 118 | + to a list? |
| 119 | + \begin{answer}[0.5in] |
| 120 | + Every existing element must be shifted one position to the |
| 121 | + right to make room at index~0, which takes time proportional |
| 122 | + to the size of the vector. A list can insert at the front |
| 123 | + in constant time by adjusting pointers. |
| 124 | + \end{answer} |
| 125 | + \end{enumerate} |
| 126 | + |
| 127 | + \vskip -20pt |
| 128 | + |
| 129 | + \Q When would you choose a {\tt list} over a {\tt vector}? \key\\[-2mm] |
| 130 | + When would you choose a {\tt vector} over a {\tt list}? |
| 131 | + \begin{answer}[0.75in] |
| 132 | + Use a \cpp{list} when you need to frequently add or remove |
| 133 | + elements from both ends, or when random access is not needed. |
| 134 | + Use a \cpp{vector} when you need fast random access by index |
| 135 | + or when you mostly add/remove from the back. |
| 136 | + \end{answer} |
| 137 | + |
| 138 | + \vskip -20pt |
| 139 | + |
| 140 | + \Q A {\tt pair} stores two related values of potentially different |
| 141 | + types and is accessed via {\tt .first} and {\tt .second}. |
| 142 | + \begin{enumerate} |
| 143 | + \itemsep 10pt |
| 144 | + \item In the pair declaration above, what is the type of |
| 145 | + {\tt student.first}? |
| 146 | + \hfill\ans[1.5in]{\cpp{string}} |
| 147 | + |
| 148 | + \item What is the type of {\tt student.second}? |
| 149 | + \hfill\ans[1.5in]{\cpp{int}} |
| 150 | + |
| 151 | + \item Write one line of C++ to declare a {\tt pair} that stores |
| 152 | + a city name (string) and its population (int), with the city |
| 153 | + {\tt "Bellingham"} and population {\tt 90000}. |
| 154 | + \begin{answer}[0.5in] |
| 155 | + \cpp{pair<string, int> city = {"Bellingham", 90000};} |
| 156 | + \end{answer} |
| 157 | + |
| 158 | + \item The file {\tt activity28b.cpp} contains the list and pair |
| 159 | + examples. Run it and verify the output matches the model. |
| 160 | + What does {\tt tasks.front()} return? |
| 161 | + \hfill\ans[1.5in]{\cpp{"breakfast"}} |
| 162 | + \end{enumerate} |
0 commit comments