Docsity

STL Trace Code Worksheet #2, Exercises of Compiler Design

Toggle sidebar
bg1
Software Design Name โ€“
STL Trace Code worksheet #2
DO NOT USE THE COMPUTER, C++ COMPILER, OR THE INTERNET FOR HELP WITH THESE EXERCISES.
SHOW SCRATCHWORK ON THIS PAPER.
Trace the following code segments and show the output.
//1
list<int> numbers;
for (int j = 0; j <= 5; j++)
numbers.insert(numbers.begin(), j);
list<int>::iterator i;
i = numbers.begin();
cout << *i << ' ';
cout << endl;
//2
deque<int> deqTest;
deqTest.push_front(1);
deqTest.push_front(2);
deqTest.push_front(3);
for (int i = 0; i < deqTest.size(); i++)
cout << deqTest[i] << ' ';
cout << endl;
//3
priority_queue<int> pq;
pq.push(10);
pq.push(9);
pq.push(20);
cout << pq.top() << " ";
cout << pq.top() << endl;
//4
pq.empty();
pq.push(1);
pq.push(9);
pq.push(2);
while (!pq.empty())
{
cout << pq.top() << " ";
pq.pop();
}
//5
priority_queue<string> pq1;
pq1.push("A");
pq1.push("B");
pq1.push("C");
while (!pq1.empty())
{
cout << pq1.top() << " ";
pq1.pop();
}

Partial preview of the text

Download STL Trace Code Worksheet #2 and more Exercises Compiler Design in PDF only on Docsity!

Software Design Name โ€“

STL Trace Code worksheet

DO NOT USE THE COMPUTER, C++ COMPILER, OR THE INTERNET FOR HELP WITH THESE EXERCISES.

SHOW SCRATCHWORK ON THIS PAPER.

Trace the following code segments and show the output. // list numbers;

for (int j = 0; j <= 5; j++) numbers.insert(numbers.begin(), j);

list::iterator i; i = numbers.begin(); cout << *i << ' '; cout << endl;

deque deqTest; deqTest.push_front(1); deqTest.push_front(2); deqTest.push_front(3);

for (int i = 0; i < deqTest.size(); i++) cout << deqTest[i] << ' ';

cout << endl;

// priority_queue pq; pq.push(10); pq.push(9); pq.push(20); cout << pq.top() << " "; cout << pq.top() << endl;

pq.empty(); pq.push(1); pq.push(9); pq.push(2);

while (!pq.empty()) { cout << pq.top() << " "; pq.pop(); }

priority_queue pq1; pq1.push("A"); pq1.push("B"); pq1.push("C");

while (!pq1.empty()) { cout << pq1.top() << " "; pq1.pop(); }