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){} 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[]={20, 30, 10, 20, 30, 10, 20, 30, 10, 20}; deque<B> d1(t, t+10); sort(d1.begin(), d1.end()); pair<deque<B> ::iterator, deque<B>::iterator > result = equal_range(d1.begin(), d1.end(), B(20)); for_each(result.first, result.second, Out<B>(cout));cout<<endl; return 0; }Program outputs:
Answer(s): B
What happens when you attempt to compile and run the following code? #include <iostream> #include <deque> #include <list> #include <queue> #include <vector> using namespace std; int main() { int t[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; deque<int> mydeck(t, t+10); list<int> mylist(t,t+10); queue<int> first; queue<int> second(mydeck); queue<int> third(second); queue<int, list<int> > fourth(mylist); mylist.clear(); third.clear(); cout<<third.size()<< " "<<mydeck.size()<< endl; cout<<fourth.size()<< " "<<mylist.size()<<endl; return 0; }
Answer(s): E
What happens when you attempt to compile and run the following code? #include <iostream> #include <deque> #include <list> #include <queue> #include <vector> using namespace std; class compare { bool reverse; public: compare(bool revparam = false) { reverse = revparam; } bool operator()(int lhs, int rhs) const { if (reverse) return (lhs > rhs); else return (lhs < rhs); } }; int main(){ int myints[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 }; priority_queue<int, deque<int> > first(myints, myints + 10); priority_queue<int, vector<int>, compare> second(myints, myints + 10, compare(false)); while (first.size() > 0){ cout << first.top() << " "; first.pop(); } while (second.size() > 0) { cout << second.top() << " "; second.pop(); } return 0; }
What happens when you attempt to compile and run the following code? #include <deque> #include <iostream> #include <algorithm> #include <set> 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 t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3}; deque<int> d1(t, t+10); set<int> s1(t,t+10); cout<<binary_search(s1.begin(),s1.end(), 4)<<" "<<binary_search(d1.begin(),d1.end(), 4)<<endl; return 0; }Choose all possible outputs (all that apply):
Answer(s): A,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<<" "; } }; struct Add { int operator()(int & a, int & b) { return a+b; } }; int main() { int t[]={1,2,3,4,5,6,7,8,9,10}; vector<int> v1(t, t+10); vector<int> v2(10); transform(v1.begin(), v1.end(), v2.begin(), bind2nd(Add(),1)); for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl; return 0; }Program outputs:
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.