Chapter 10 Exercise Set 1: CPE PracticeΒΆ
What is the output of the following program?
#include <iostream> #include <vector> using namespace std; int main() { vector<char> text(5); char *chp1 = text.data() + 2, *chp2 = chp1 + 3; cout << chp2 - text.data() << endl; return 0; }
What is the output of the following program?
#include <iostream> #include <vector> using namespace std; int main() { vector<double> a = {1e-1, 1e0, 1e1}; double *dp = a.data() + 2; cout << a[1] - *dp << endl; return 0; }
What is the output of the following program?
#include <iostream> #include <vector> using namespace std; int main() { vector<float *> fpv = {new float[1], new float[1], new float[1]}; for (int i = 0; i < 3; i++) { float *fp = fpv[i]; *fp = 2 * i + 1; } for (int i = 0; i < 3; i++) { cout << *fpv[i]; delete[] fpv[i]; } cout << endl; return 0; }
What is the output of the following program?
#include <iostream> #include <vector> using namespace std; void swap(int* ip1, int* ip2) { int n = *ip1; int m = *ip2; } int main() { vector<int> a = {3, 2, 1}; swap(&a[0], &a[1]); swap(&a[2], &a[0]); cout << a[0] << ' ' << a[1] << ' ' << a[2] << endl; return 0; }
What is the output of the following program?
#include <iostream> #include <vector> using namespace std; void swap(int* ip1, int* ip2) { int n = *ip1; *ip1 = *ip2; *ip2 = n; } int main() { vector<int> a = {3, 2, 1}; swap(&a[0], &a[1]); swap(&a[2], &a[0]); cout << a[0] << ' ' << a[1] << ' ' << a[2] << endl; return 0; }