Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Quizzes  arrow Chapter 19

Chapter 19

Standard Template Library

This activity contains 10 questions.

Question 1.
Which of the following programs will correctly define an iterator and print the contents of the vector?

 
End of Question 1


Question 2.

The following code creates a vector of integers. Which snippet does not correctly output the data in the vector?

#include <iostream>
#include <vector>
using namespace std;

// Main
void main()
{
	vector<int> myData;
	vector<int>::iterator p;

	myData.push_back(3);
	myData.push_back(4);
	myData.push_back(5);

	// CODE TO OUTPUT DATA HERE
}
 
End of Question 2


Question 3.

What will the following program output when run?

void main()
{
	vector<int> myData;
	vector<int>::iterator p;

	myData.push_back(2);
	myData.push_back(4);
	myData.push_back(6);

	p = myData.begin();
	cout << *(p+2) << endl;
	myData[2] = 5;
	cout << *(p+2) << endl;
}
 
End of Question 3


Question 4.
What will the following program output when run?

void main()
{
	vector<int> myData;
	vector<int>::iterator p;

	myData.push_back(2);
	myData.push_back(4);
	myData.push_back(6);

	p = myData.end();
	while (p!=myData.begin()) {
		cout << *p << endl;
		p--;
	}
}

 
End of Question 4


Question 5.

What will the following program output when run?

void main()
{
	vector<int> myData;
	vector<int>::reverse_iterator p;

	myData.push_back(2);
	myData.push_back(4);
	myData.push_back(6);

	p = myData.rend()-1;
	while (p!=myData.rbegin())
	{
		cout << *p << endl;
		p--;
	}
	cout << *p << endl;
}
 
End of Question 5


Question 6.

What is the output of the following program when run?

#include <iostream>
#include <map>
#include <string>
using namespace std;

void main()
{
	map<string,string> idMap;
	map<string,string>::iterator iter;

	idMap["501-10-3011"] = "DeBanque, Robin";
	idMap["301-10-3012"] = "Wright, Eaton";
	idMap["201-10-3010"] = "Oki, Cary";

	for (iter = idMap.begin(); iter != idMap.end(); iter++)
	{
		cout << iter->first << " " << iter->second << endl;
	}
}
 
End of Question 6


Question 7.

Which function will correctly find the name in the map that matches the target? The output of the program should be:

Wright, Eaton
Name not found

#include <iostream>
#include <map>
#include <string>
using namespace std;

// Main
void main()
{
	map<string,string> idMap;

	idMap["501-10-3011"] = "DeBanque, Robin";
	idMap["301-10-3012"] = "Wright, Eaton";
	idMap["201-10-3010"] = "Oki, Cary";

	cout << GetName(idMap,"301-10-3012") << endl;
	cout << GetName(idMap,"999-99-9999") << endl;
}
 
End of Question 7


Question 8.
Rank the following runtime estimates in terms of their Big-O notation. Give the smallest asymptotically growing function first, the largest last.

2^ (log n)
2000(n^2) + log n
2^n
(log n)^2

 
End of Question 8


Question 9.

The following two functions will both work correctly to retrieve a name from a STL map. What is the Big-O runtime of each?

string GetName1(map<string,string> idMap, string targetKey)
{
	map<string,string>::iterator iter;

	iter = idMap.find(targetKey);
	if (iter == idMap.end()) {
		return("Name not found.");
	}
	else {
		return (iter->second);
	}
}

string GetName2(map<string,string> idMap, string targetKey)
{
	map<string,string>::iterator iter;
	for (iter = idMap.begin(); iter != idMap.end(); iter++) {
		if (iter->first == targetKey) return iter->second;
	}
	return("Name not found");
}
 
End of Question 9


Question 10.
Why might a O(N) algorithm be preferred to a O(log N) algorithm?

 
End of Question 10





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page