CHAPTER 4 - STL
Attempt History
Attempt Time Score
KEPT Attempt 2 26 minutes 26 out of 27
LATEST Attempt 2 26 minutes 26 out of 27
Attempt 1 24 minutes 23.5 out of 27
Correct answers are hidden.
Score for this attempt: 26 out of 27
Submitted Dec 24 at 9:50pm
This attempt took 26 minutes.
Question 1
pts
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printer(int i) {
cout << i << ", ";
}
int main() {
int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
vector<int> v1(mynumbers, mynumbers + 7);
copy(mynumbers, mynumbers + 7, v1.end());//LINE I
for_each(v1.begin(), v1.end(), printer);//LINE II
return 0;
}
program outputs: 3, 9, 0, 2, 1, 4, 5,
program outputs: 3, 9, 0, 2, 1, 4,
runtime error at LINE I
,program outputs: 0, 1, 2, 3, 4, 5, 9,
compilation error in LINE I
runtime error at LINE II
compilation error in LINE II
Question 2
pts
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void printer(int i) {
cout << i << ", ";
}
int main() {
int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
vector<int> v1(mynumbers, mynumbers + 7);
copy_backward(mynumbers, mynumbers + 7, v1.rend());//LINE I
for_each(v1.begin(), v1.end(), printer);//LINE II
return 0;
}
runtime error at LINE II
compilation error in LINE I
program outputs: 3, 9, 0, 2, 1, 4,
,program outputs: 5, 4, 1, 2, 9, 0,
compilation error in LINE II
runtime error at LINE I
program outputs: 5, 4, 1, 2, 0, 9, 3,
program outputs: 3, 9, 0, 2, 1, 4, 5,
Question 3
pts
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
void printer(int i) {
cout << i << ", ";
}
int main() {
int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
set<int> s1(mynumbers, mynumbers + 7);
vector<int> v1(s1.rbegin(), s1.rend());
swap(s1, v1);//LINE I
for_each(v1.begin(), v1.end(), printer);//LINE II
return 0;
}
program outputs: 0, 1, 2, 3, 4, 5, 9,
, program outputs: 3, 9, 0, 2, 1, 4,
program outputs: 0, 1, 2, 3, 4, 5,
compilation error in LINE I
runtime error at LINE II
runtime error at LINE I
compilation error in LINE II
program outputs: 3, 9, 0, 2, 1, 4, 5,
Question 4
pts
What will happen when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
void printer(int i) {
cout << i << ", ";
}
int main() {
int mynumbers[] = { 3, 9, 0, 2, 1, 4, 5 };
deque<int> d1(mynumbers, mynumbers + 7);
vector<int> v1(d1.rbegin(), d1.rend());
swap_ranges(v1.begin(), v1.end(), d1.begin());//LINE I
sort(d1.begin(), d1.end());//LINE II