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() { B t1[]={3,2,4,1,5}; int t2[]={5,6,8,2,1}; vector<B> v1(10,0); sort(t1, t1+5); sort(t2, t2+5); set_union(t1,t1+5,t2,t2+5,v1.begin()); for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<endl; return 0; }Program outputs:
Answer(s): E
What happens when you attempt to compile and run the following code? #include <iostream> #include <map> #include <vector> #include <sstream> #include <string> using namespace std; int main(){ int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; vector<int> v(t, t+10); map<int,string> m; for(vector<int>::iterator i=v.begin(); i!=v.end(); i++) { stringstream s; s<<*i<<*i; m.insert(pair<int,string>(*i,s.str())); } for(map<int, string>::iterator i=m.begin();i!= m.end(); i++) { cout<<*i<<" "; } return 0; }
What will happen when you attempt to compile and run the code below, assuming that file test.in contains the following sequence: 1 2 3? #include <iostream> #include <fstream> #include <string> #include <list> #include <algorithm> using namespace std; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) {out<<val<<" "; } }; int main () { ifstream f("test.in"); list<int> l; for( ; !f.fail() ; ) { int i; f>>i; l.push_back(i); } f.close(); for_each(l.begin(), l.end(), Out<int>(cout)); return 0; }Programwill output:
Answer(s): B
What happens when you attempt to compile and run the following code? #include <vector> #include <iostream> #include <algorithm> using namespace std; template<class T>struct Out { ostream & out; Out(ostream & o): out(o){} void operator() (const T & val ) { out<<val<<" "; } }; int main() { int t1[]={3,2,4,1,5}; int t2[]={6,10,8,7,9}; vector<int> v1(10); sort(t1, t1+5); sort(t2, t2+5); copy(t1,t1+5,v1.begin()); copy(t2,t2+5,v1.begin()+5); merge(v1.begin(), v1.begin()+5,v1.end()); for_each(v1.begin(), v1.end(), Out<int>(cout));cout<<endl; return 0; }Program outputs:
What happens when you attempt to compile and run the following code? #include <vector> #include <set> #include <iostream> #include <algorithm> using namespace std; void print(int v) { cout<<v<<" "; } struct Sequence { int start; Sequence(int start):start(start){} int operator()() { return start++; } }; bool predicate(int v) { return v%2==0; } int main() { vector<int> v1(10); generate_n(v1.begin(), 10, Sequence(1)); set<int> s1(v1.begin(), v1.end()); remove_if(s1.begin(), s1.end(), predicate); for_each(s1.begin(), s1.end(), print);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.