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;} }; 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}; B t2[]={5,6,8,2,1}; vector<B> v1(10,0); sort(t1, t1+5); sort(t2, t2+5); set_difference(t1,t1+5,t2,t2+5,v1.begin()); for_each(v1.begin(), v1.end(), Out<B>(cout));cout<<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){}
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:
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 t[]={3,2,4,1,5,10,9,7,8,6};
vector<int> v1(t,t+10);
cout<<*max_element(v1.begin(), v1.end());
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
int main() {
string t[]={"aaa","Aaa", "aAa","aaA","bbb","Bbb", "bBb", "bbB"};
vector<string> v1(t, t+8);
sort(v1.begin(), v1.end());
for_each(v1.begin(), v1.end(), Out<string>(cout));cout<<endl;
return 0;
}
Program outputs:
What will happen when you attempt to compile and run the following code? Choose all that apply.
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
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 < (const A & b) const { return a<b.a;}
};
class F {
A val;
public:
F(A & v):val(v){}
bool operator() (A & v) {
if (v.getA() == val.getA()) return true;
return false;
}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<A> v1(t, t + 10);
set<A> s1(t, t + 10);
A a(6); F f(a);
find_if(s1.begin(), s1.end(), f);
if (find_if(v1.begin(), v1.end(), f) !=v1.end()) {
cout<<"Found!\n";
} else {
cout<<"Not found!\n";
}
return 0;
}