What happens when you attempt to compile and run the following code?
#include
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int t[] = {1, 2 ,3 ,4 ,5};
vector<int>v1(t, t+5);
deque<int>d1;
d1.assign(v1.end(), v1.begin());
for(int i=0; i { cout<<d1.at(i)<<" "; } cout<<endl; return 0; }
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool compare(int a, int b) { return a == b; }
int main () {
int t[] = {1,2,3,4,5,1,2,3,4,5};
vector<int> v (t,t+10);
vector<int>::iterator it = v.begin();
int m1[] = {1, 2, 3};
while ( (it = find_first_of (it, v.end(), m1, m1+3)) != v.end()) {
cout<<it?v.begin()<<" ";
}
cout<< endl;
return 0;
}
What happens when you attempt to compile and run the following code?
#include <iostream>
#include
using namespace std;
int main() {
int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
string s[] = { "one", "one", "two", "two", "three","three", "four", "four", "five", "five"};
map<int, string> m;
for (int i = 0; i < 10; i++) {
m.insert(pair<int, string>(t[i], s[i]));
}
if (m.count(3) == 2) {
m.erase(3);
}
for (map<int, string>::iterator i = m.begin(); i != m.end(); i++) {
cout << i?>first << " ";
}
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add : public binary_function<int, int, int> {
int operator() (const int & a, const int & b) const {
return a+b;
}
};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
deque<int> d1(t, t+10);
deque<int> d2(10);
transform(d1.begin(), d1.end(), d2.begin(), bind2nd(Add(), 1));
for_each(d2.rbegin(), d2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include <vector>
#include
#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<<" "; } };
template <typename T> struct Sequence {
T start; T step;
Sequence(T start, T step):start(start), step(step){}
T operator()() { T v = start; start+=step; return v; } };
bool Less(float a, float b) { return int(a)<int(b);}
int main() {
float t[]={2.28, 1.66, 1.32, 3.94, 3.64, 2.3, 2.98, 1.96, 2.62, 1.13};
vector
stable_sort(v1.begin(), v1.end(), Less);
for_each(v1.begin(), v1.end(), Out
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;
void print(int v) {
cout<<v<<" ";
}
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() {
return start++;
}
};
int main() {
vector<int> v1(10);
generate_n(v1.begin(), 10, Sequence(1));
for_each(v1.begin(), v1.end(), print);
cout<<endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include
#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};
deque d1(t, t+10);
sort(d1.begin(), d1.end());
deque::iterator it = upper_bound(d1.begin(), d1.end(), B(4), greater());
for_each(it, d1.end(), Out(cout)); cout<<endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
void g(int a)
{
cout<<a?1< } template<class A> void g(A a) { cout<<a+1< } int main() { int a = 1; g(a); return 0; }
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include
#include
using namespace std;
void myfunction(int i) {
cout << " " << i;
}
int add (int a, int b) { return a+b; }
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t+10);
set<int> s1(t, t+10);
deque<int> d1;
d1.resize(s1.size());
transform(s1.begin(), s1.end(), v1.begin(), d1.begin(), add);
for_each(d1.begin(), d1.end(), myfunction);
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include
#include <iostream>
using namespace std;
bool mycomparison (int first, int second){return first>second;}
template<class T>
void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main()
{
int t1[] ={ 1, 7, 8, 4, 5 };
list<int> l1(t1, t1 + 5);
int t2[] ={ 3, 2, 6, 9, 0 };
list<int> l2(t2, t2 + 5);
l1.sort(mycomparison);
l2.sort(mycomparison);
l1.merge(l2,mycomparison);
print(l1.begin(), l1.end());
print(l2.begin(), l2.end()); cout<<endl;
return 0;
}
What happens when you attempt to compile and run the following code?
#include <vector>
#include <iostream>
using namespace std;
class A
{
int a,b;
public:
A(const A & c) { a = c.a; }
A():a(0),b(0){}
void setA(int a) {this?>a = a;} void setB(int b) {this?>b = b;}
int getA() {return a;} int getB() {return b;}
};
int main ()
{
vector<A>v;
A a;
a.setA(10); a.setB(11);
v.push_back(a);
cout<<v[0].getB()<<" "<<v[0].getA()< 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
#include
#include
#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:
What happens when you attempt to compile and run the following code?
#include
#include <iostream>
#include <algorithm>
#include
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)<<" "< return 0; } Choose all possible outputs (all that apply):
What happens when you attempt to compile and run the following code?
#include
#include
#include <iostream>
using namespace std;
template<class T> void print(T start, T end) {
while (start != end) {
std::cout << *start << " "; start++;
}
}
int main() {
string t1[] ={ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
list
list
l2.reverse(); l1.splice(l1.end(),l2);
l1.unique();
print(l1.begin(), l1.end()); cout<<endl;
return 0;
}
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include <vector>
#include
#include
using namespace std;
struct display {
void operator() (int i) {cout << " " << i;}
};
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<int> v1(t, t + 10);
deque<int> d1(t, t + 10);
set<int> s1(t, t + 10);
for_each(v1.begin(), v1.end(), display); //Line I
for_each(d1.begin(), d1.end(), *(new display())); // Line II
for_each(s1.begin(), s1.end(), display()); // Line III
return 0;
}
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include
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; }
};
int main () {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
deque<int> d (t,t+15);
int number = count(d.begin(), d.end(), 2);
cout<< number< return 0; } Program outputs:
What happens when you attempt to compile and run the following code?
#include
#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[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
deque<int> d1(t, t+10);
deque<int>::iterator it = lower_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.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 <iostream>
#include <algorithm>
#include <functional>
using namespace std;
template<class T>struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out<<val<<" "; } };
struct Add : public binary_function<int, int, int> {
int operator() (const int & a, const int & b) const {
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(), bind1st(Add(), 1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs:
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 64 100
#include <iostream>
#include
#include
#include
using namespace std;
int main ()
{
string s;
getline(cin, s);
stringstream input(s);
stringstream output;
for( ; !input.fail() ; )
{
int i;
input>>hex>>i;
output< } cout<<output.str(); return 0; } What will be the result assuming that user will enter following sequence: 64 100:
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[]={5,6,8,2,1};
vector<int> v1(10);
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<int>(cout));cout<<endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include <iostream>
#include <algorithm>
#include
using namespace std;
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
map<int, int> m;
for(int i=0; i < 10; i++) {
m[i]=t[i];
}
map<int, int>::iterator it = find(m.begin(), m.end(), 5);
cout<<it?>first;
return 0;
}
Program outputs:
Which sentence is correct about the code below?
#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; }
/* Insert Code Here */
};
struct add10 { void operator()(A & a) { a.setA(a.getA() + 10); } };
int main() {
int t[] = { 10, 5, 9, 6, 2, 4, 7, 8, 3, 1 };
vector<A> v1(t, t + 10);
for_each(v1.begin(), v1.end(), add10());
vector<A>::iterator it = find(v1.begin(), v1.end(), A(7));
cout << it?>getA() << endl;
return 0;
}
What happens when you attempt to compile and run the following code?
#include <iostream>
using namespace std;
template<class A>
void f(A a)
{
cout<<1< } void f(int a) { cout<<2< } int main() { int a = 1; f return 0; }
What happens when you attempt to compile and run the following code?
#include
#include <vector>
#include <iostream>
using namespace std;
int main ()
{
int t[] = {1, 2 ,3 ,4 ,5};
vector<int>v1(t, t+5);
list<int>l1;
l1.assign(v1.end(), v1.begin());
for(int i=0; i { cout<<l1.at(i)<<" "; } cout<<endl; return 0; }
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 v1(t, t+10);
for_each(v1.begin(), v1.end(), bind1st(plus(), 1));
for_each(v1.rbegin(), v1.rend(), Out(cout));cout<<endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code? Choose all that apply.
#include <iostream>
#include
#include
#include
#include <algorithm>
#include
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< int main () { int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; fstream f("test.out", ios::trunc|ios::out); list l(t, t+10); for_each(l.begin(), l.end(), Out(f)); f.close(); f.open("test.out"); for( ; f.good() ; ) { int i; f>>i; cout<<i<<" "; } f.close(); return 0; }
What will be output of the program when you attempt to compile and run the following code?
#include <iostream>
#include
#include <vector>
#include
using namespace std;
int main(){
int second[] ={ 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
string first[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","zero"};
multimap<int,string> m;
for(int i=0; i<10; i++) {
m.insert(pair<int,string>(second[i],first[i]));
}
m[0]="ten";
m.insert(pair<int,string>(1,"eleven"));
for(multimap<int, string>::iterator i=m.begin();i!= m.end(); i++) {
cout<<i?>second<<" ";
}
return 0;
}
What happens when you attempt to compile and run the following code?
#include
#include <vector>
#include <iostream>
using namespace std;
template<typename T>
int calculate(T start, T end)
{
int s = 0;
while (start != end)
s+= *start; start++;return s;
}
int main ()
{
int t[] = {1, 2 ,3 ,4 ,5, 6 , 7, 8 , 9, 10};
vector<int>v1(t, t+5);
deque<int>d1(t+5, t+10);
cout<<calculate(t,t+10)<<" ";
cout<<calculate(v1.begin()+1,v1.end()?2)<<" ";
cout<<calculate(d1.rbegin()+1,d1.rend()?2)<<" ";
cout<<calculate(t[0],t[10])<<" ";
cout<<endl;
return 0;
}
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(), bind1st(Add(),1));
for_each(v2.rbegin(), v2.rend(), Out<int>(cout));cout<<endl;
return 0;
}
Program outputs: