What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> #include <algorithm> using namespace std; class B { int val; public: B(int v):val(v){} int getV() const {return val;} bool operator < (const B & v) const { return val<v.val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; vector<B> v1(t, t+10); sort(v1.begin(), v1.end(), greater<B>()); for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl; return 0; }Program outputs:
Answer(s): C
What happens when you attempt to compile and run the following code? #include <iostream> using namespace std; template <class T> class A { T _v; public: A() {} A(T v): _v(v){} friend ostream & operator<<(ostream & c, const A<T> & v); }; template <class T> ostream & operator<<(ostream & c, const A<T> & v) { c<<v._v; return c; } int main() { A<int> a(10); cout<<a<<endl; return 0; }
Answer(s): B
What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> #include <algorithm> #include <functional> using namespace std; class B { int val; public: B(int v=0):val(v){} int getV() const {return val;} operator int () const { return val;} }; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { B t[]={3,2,4,1,5,6,10,8,7,9}; vector<B> v1(t, t+10); for_each(v1.begin(), v1.end(), bind1st(plus<B>(), 1)); for_each(v1.rbegin(), v1.rend(), Out<B>(cout));cout<<endl; return 0; }Program outputs:
What happens when you attempt to compile and run the following code? #include <iostream> #include <algorithm> #include <vector> using namespace std; class A { int a; public: A(int a) : a(a) {} int getA() const { return a; } void setA(int a) { this?>a = a; } bool operator==(A & b) { return a == b.a; } }; struct Compare{ bool operator()(const A & a, const A & b) {return a.getA()==b.getA();}; }; int main () { int t[] = {1,2,3,4,5,1,2,3,4,5}; vector<A> v (t,t+10); vector<A>::iterator it; A m1[] = {A(1), A(2), A(3)}; it = search (v.begin(), v.end(), m1, m1+3, Compare()); cout << "First found at position: " << it?v.begin() << endl; return 0; }Program outputs:
What happens when you attempt to compile and run the following code? #include <deque> #include <iostream> #include <algorithm> using namespace std; class B { int val; public: B(int v):val(v){} B(){} int getV() const {return val;} }; ostream & operator <<(ostream & out, const B & v) { out<<v.getV(); return out;} template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; deque<B> d1(t, t+10); deque<B>::iterator it = lower_bound(d1.begin(), d1.end(), 4); for_each(it, d1.end(), Out<B>(cout));cout<<endl; return 0; }Program outputs:
Answer(s): D
Post your Comments and Discuss C++ Institute CPP exam prep with other Community members:
Our website is free, but we have to fight against AI bots and content theft. We're sorry for the inconvenience caused by these security measures. You can access the rest of the CPP content, but please register or login to continue.