a. vector b. list c. deque d. map e. set f. multimap g. multiset h. hash table
int a, b; int *x, *y;Also suppose that the above variables are assigned the following memory addresses:
a: 0x1000 b: 0x1004 x: 0x1008 y: 0x100cAfter the following code sequence executes, what are the values of a, b, x, and y?
x = &b; y = x; a = 10; b = 40; *x = 30; *y = *x * 3; a: 10 b: 90 x: 0x1004 y: 0x1004
int *x; x = new int; *x = 40; x = new int; *x = 60;The code fails to delete the original heap-allocated piece of memory to which x pointed, and as a result, that piece of memory never gets deleted even though it no longer is accessable because it has no pointers to it.
class PersonNode { public: string name; PersonNode *next; PersonNode(string n) : name(n), next(NULL) {} } PersonNode *x; PersonNode *y; PersonNode *newnode;
newnode = new PersonNode("Frank"); x = newnode; newnode = new PersonNode("Jackie"); newnode->next = x; x = newnode;
newnode = new PersonNode("Sue"); y = x->next; newnode->next = y; x->next = newnode;
map<string, set<string> > friendsMap; map<string, set<string> >::iterator friends_it; set<string> friendsSet; string friend1, friend2; while (cin >> friend1 >> friend2) { friends_it = friendsMap.find(friend1); friendsSet = friends_it->second; friendsSet.insert(friend2); }I inadvertently left out code that checked to see if friend1 was in the set. That meant that the code could have two issues, and I accepted either of the two issues as the correct answer. I meant for the correct answer to be the second issue. The bold-faced line in the above code has two issues. The first issue is that if friend1 is not in the set, then friends_it points to a sentinel node and when you try to access the second field, you will not retrieve a valid set. That means that the insert call in the following statement will seg fault. The second issue is that the bold-faced statement creates a copy of the set. The insert on the next line thus inserts into the copy, rather than the original set, and the original set does not get updated as it should.
class Person { public: string name; int age; }; Person *p; p->name = "Nancy"; p->age = 25;Answer the following questions about the code fragment:
p is never initialized with a pointer to a Person object. As a result when the code attempts to assign "Nancy" and 25 to the name and age fields, it is trying to go through an invalid pointer. At some point this will most likely cause your program to seg fault.
After the declaration for p and before the two assignment
statements, you need the following statement:
p = new Person();
Algorithm 1: T(n) = 60n3 + 5n*log n + 50000n + 20 Algorithm 2: T(n) = 0.10 * 2n + n + 8
multimap<int, string> salaries;The key is an integer denoting the person's salary and the value is a string representing the person's name. Below are two code fragments which will correctly print out the names and salaries. lower_bound(15000) returns an iterator that points to the first element in the multimap whose salary is greater than or equal to 15,000. lower_bound() has the same Big-O running time as the find function.
(a) | (b) |
---|---|
multimap<int, string>::iterator mit; for (mit = m.begin(); mit != m.end(); mit++) { if (mit->first >= 15000 && mit->first <= 20000) { cout << mit->second << " " << mit->first << endl; } } |
multimap<int, string>::iterator mit; mit = m.lower_bound(15000); for (; mit != m.end(); mit++) { if (mit->first > 20000) return; else cout << mit->second << " " << mit->first << endl; } |
Make the following assumptions:
Answer the following questions:
list<string> names; list<string>::iterator nit, nit1; list<string>::reverse_iterator rnit; names.push_back("joe"); names.push_back("mary"); names.push_front("nancy"); names.push_front("tom"); nit = names.begin(); nit++; rnit = names.rbegin(); nit1 = names.end();
class User { public: string name; int weight; };
double avgWeight(list<User *> &data) { int sum = 0; list<User *>::iterator lit; for (lit = data.begin(); lit != data.end(); lit++) sum += (*lit)->weight; return sum / (double)data.size(); }
Your job is to write a program that prints out the name of the winning person(s) and the number of problems they were credited with solving. Put a single space between the winner's name and the number of problems they solved. If there is a tie, then print the names of all of the winning persons in alphabetical order with each person printed on a new line.
Here is a straightforward solution that uses a find to determine
if a problem or contestant has been previously seen and if not, then
an insert to insert the problem or contestant into the appropriate
data structure:
#include <map>
#include <set>
#include <iostream>
using namespace std;
int main() {
set<string> problems;
map<string, int> contestants;
map<string, int>::iterator mit;
int max;
string name, problem;
while (cin >> name >> problem) {
// determine if a problem has been previously solved. If it has not
// been previously solved then insert the problem into the problems
// set and add 1 to the contestants score. If the contestant has not
// been previously seen, then insert the contestant into the contestants
// map and assign the contestant a score of 1.
if (problems.find(problem) == problems.end()) {
problems.insert(problem);
mit = contestants.find(name);
if (mit != contestants.end()) {
mit->second++;
}
else
contestants[name] = 1;
}
}
// find the winning score
max = 0;
for (mit = contestants.begin(); mit != contestants.end(); mit++) {
if (mit->second > max) {
max = mit->second;
}
}
// print all winners in alphabetical order
for (mit = contestants.begin(); mit != contestants.end(); mit++) {
if (mit->second == max) {
cout << mit->first << " " << mit->second << endl;
}
}
return 0;
}
Here is a slightly more efficient version for scoring the
contestant that combines the insert and find operations. It's
a bit more complicated to read but performs only one O(log n)
operation rather than two O(log n) operations. It takes advantage
of the fact that insert for both sets and maps returns a pair
where the first element is an iterator either to the inserted
node or, if the key already existed, to the node for the pre-existing
key, and the second element is a bool indicating whether or not
the insert succeeded.
while (cin >> name >> problem) {
// If the insert succeeds, the second item in the return result will
// be true.
problemsInsertResult = problems.insert(problem);
if (problemsInsertResult.second) {
// If the insert succeeds, then a new node with a contestant score
// of 0 is added to the map. If the insert fails, then insert returns
// an iterator to the existing node. In either insert returns
// an iterator to the contestant's node and we can
// then increment the contestant's score by 1
contestantsInsertResult = contestants.insert(make_pair(name, 0));
mit = contestantsInsertResult.first;
mit->second++;
}
}