Section 27.3 Check Point Questions3 questions
27.3.1
What is the output of the following code?
vector<WeightedEdge> v; v.push_back(WeightedEdge(1, 2, 3.5)); v.push_back(WeightedEdge(1, 6, 6.5)); v.push_back(WeightedEdge(1, 7, 1.5)); cout << v[0].weight << endl; cout << v[1].weight << endl; cout << v[2].weight << endl;
27.3.2
What is the output of the following code?
priority_queue<WeightedEdge, vector<WeightedEdge>, greater<WeightedEdge>> q; q.push(WeightedEdge(1, 2, 3.5)); q.push(WeightedEdge(1, 6, 6.5)); q.push(WeightedEdge(1, 7, 1.5)); cout << q.top().weight << endl; q.pop(); cout << q.top().weight << endl; q.pop(); cout << q.top().weight << endl;
27.3.3
What is wrong in the following code? Fix it and show the output.
vector<vector<WeightedEdge>> neighbors; neighbors[0].push_back(WeightedEdge(0, 2, 3.5)); neighbors[0].push_back(WeightedEdge(0, 6, 6.5)); neighbors[0].push_back(WeightedEdge(0, 7, 1.5)); neighbors[1].push_back(WeightedEdge(1, 0, 3.5)); neighbors[1].push_back(WeightedEdge(1, 5, 8.5)); neighbors[1].push_back(WeightedEdge(1, 8, 19.5)); cout << neighbors[0][0] << endl;