STL Trace Code Worksheet #2, Exercises of Compiler Design

A series of exercises that require tracing code segments and showing the output. The exercises involve the use of various STL containers such as list, deque, and priority_queue. The document explicitly states that no external help should be used and scratchwork should be shown on paper. The exercises are designed to test the student's understanding of the STL containers and their methods.

Typology: Exercises

2021/2022

Uploaded on 05/11/2023

weldon
weldon 🇺🇸

4.5

(10)

223 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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(); }