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 < < endl;
}
void f(int & a)
{
cout < < 2 < < endl;
}
int main()
{
int a = 1;
f(a);
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() {
char s[]={"qwerty"};
char t1[]={"ert"};
char t2[]={"ERT"};
sort(s, s+6);
cout < < includes(s,s+6, t1,t1+3) < < " " < < includes(s,s+6, t2,t2+3) < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < deque >
#include < vector >
#include < iostream >
using namespace std;
int main ()
{
int t[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
vector < int > v1(t, t + 10);
deque < int > d1(v1.begin(), v1.end());
deque < int > d2;
d2 = d1;
d2.insert(d1.rbegin(), 10);
for(int i = 0; i < d1.size(); i++)
{
cout < < d1[i] < < " ";
}
return 0;
}
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < algorithm >
#include < vector >
#include < set >
using namespace std;
int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
vector < int > v1(t, t + 15);
set < int > s1(t, t + 15);
pair < set < int > ::iterator, vector < int > ::iterator > resultSet = mismatch(s1.begin(), s1.end(), v1.begin());
cout < < *resultSet.first < < " " < < *resultSet.second < < endl;
return 0;
}
Program outputs:
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;
}
What will happen when you attempt to compile and run the following code?
#include < iostream >
#include < set >
#include < vector >
using namespace std;
int main(){
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector < int > v(t, t+10);
set < int > s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
bool found = s1.find(7);
if (found){
cout < < "Element found!\n";
}else {
cout < < "Element not found!\n";
}
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;
template < class T > struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) { out < < val < < " "; } };
int Add(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(ptr_fun (Add),1));
for_each(v2.rbegin(), v2.rend(), Out < int > (cout));cout < < endl;
return 0;
}
Program outputs:
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, 0, 1, 2, 3, 4, 0 };
vector < int > v(t, t+10);
multimap < 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(multimap < int, string > ::iterator i=m.begin();i!= m.end(); i++) {
cout < < *i < < " ";
}
return 0;
}
What happens when you attempt to compile and run the following code?
#include < iostream >
using namespace std;
int main()
{
cout < < true < < " " < < boolalpha < < false;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < vector >
#include < iostream >
int main ()
{
int t[]={1,2,3,4,5};
std::vector < int > v1(t,t+5);
std::vector < int > v2(v1);
v1.resize(10);
v2.reserve(10);
std::vector < int > ::iterator i = v1.begin(); int ii = 0;
while (i != v1.end()) { std::cout < < i[ii] < < " ";ii??;i++; }
i = v2.begin();ii=0;
while (i != v2.end()) { std::cout < < i[ii] < < " ";ii??;i++; }
return 0;
}
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 t[]={3,2,4,1,5,10,9,7,8,6};
vector < B > v1(t,t+10);
sort(v1.begin(), v1.end(), greater < B > ());
cout < < *min_element(v1.begin(), v1.end());
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;
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 >
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:
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: true false < enter > ?
#include < iostream >
#include < string >
using namespace std;
int main ()
{
bool a,b;
cin > > boolalpha > > a > > b;
cout < < a < < b < < endl;
return 0;
}
Program will output:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < map >
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.push_back(pair < int, string > (t[i], s[i] ));
}
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 < deque >
#include < set >
#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 < B > d1(t, t+10);
sort(d1.begin(), d1.end());
set < B > s1(t,t+10);
cout < < binary_search(s1.begin(),s1.end(), 4) < < " " < < binary_search(d1.begin(),d1.end(), 4) < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < vector >
using namespace std;
int main ()
{
std::vector < int > v1;
v1.push_back(10);
return 0;
}
What happens when you attempt to compile and run the following code?
#include < deque >
#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 Sequence { int start;
Sequence(int start):start(start){}
int operator()() {return 10*(1+(start++ %3));}
};
int main() {
deque < int > d1(10);
generate(d1.begin(), d1.end(), Sequence(1));
sort(d1.begin(), d1.end());
pair < deque < int > ::iterator, deque < int > ::iterator > result = equal_range(d1.begin(), d1.end(), 20);
for_each(result.first, result.second, 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 < deque >
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 < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < algorithm >
#include < deque >
#include < vector >
using namespace std;
bool identical(int a, int b) {
return b == 2*a?true:false;
}
int main() {
int t[] = {1,2,3,2,3,5,1,2,7,3,2,1,10, 4,4,5};
int u[] = {2,4,6,4,6,10,2,4,14,6,4,2,20,8,8,5};
vector < int > v1(t, t + 15);
deque < int > d1(u, u + 15);
pair < deque < int > ::iterator, vector < int > ::iterator > result;
result = mismatch(d1.begin(), d1.end(), v1.begin(), identical); //Line I
if (result.first == d1.end() & & result.second == v1.end()) { //Line II
cout < < "Identical\n";
} else {
cout < < "Not identical\n";
}
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code? Choose all that apply.
#include < iostream >
#include < fstream >
#include < string >
#include < list >
#include < algorithm >
#include < iomanip >
using namespace std;
template < class T > struct Out {
ostream & out;
Out(ostream & o): out(o){}
void operator() (const T & val ) {out < < setw(3) < < hex < < val; } };
int main () {
int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
fstream f("test.out", ios::trunc|ios::out);
list < int > l(t, t+10);
for_each(l.begin(), l.end(), Out < int > (f));
f.close(); f.open("test.out");
for( ; f.good() ; ) {
int i; f > > i;
cout < < i < < " ";
}
f.close();
return 0;
}
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < algorithm >
#include < map >
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] ;
}
pair < const int,int > p(5,5);
map < int, int > ::iterator it = find(m.begin(), m.end(), p);
if (it != m.end())
{
cout < < it? > first < < endl;
}
else
{
cout < < "Not found!\n";
}
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < map >
#include < vector >
#include < string >
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"};
map < 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(map < 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 < deque >
#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);
sort(d1.begin(), d1.end());
deque < int > ::iterator it = upper_bound(d1.begin(), d1.end(), 4);
for_each(it, d1.end(), Out < int > (cout));cout < < endl;
return 0;
}
Program outputs:
What will happen when you attempt to compile and run the following code?
#include < iostream >
using namespace std;
template < class T >
class A {
T _v;
public:
A(T v);
};
template < class T >
Which changes, introduced independently, will allow the code to compile and display “one” “eight” “nine” “ten”? Choose all that apply
#include < iostream >
#include < map >
#include < string >
using namespace std;
class A {
int a;
public:
A(int a):a(a){}
int getA() const { return a;}
/* Insert Code Here 1 */
};
/* Insert Code Here 2 */
int main(){
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 10 };
string s[] = {"three", "four", "two", "one", "six","five", "seven", "nine","eight","ten"};
map < A, string > m; /* Replace Code Here 3 */
for(int i=0; i < 10; i++) {
m.insert(pair < A,string > (A(t[i]),s[i] ));
}
m.erase(m.lower_bound(2),m.upper_bound(7));
map < A, string > ::iterator i=m.begin(); /* Replace Code Here 4 */
for( ;i!= m.end(); i++) {
cout < < i? > second < < " ";
}
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):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 t[]={3,2,4,1,5,10,9,7,8,6};
vector < B > v1(t,t+10);
cout < < *max_element(v1.begin(), v1.end(), greater < B > ());
cout < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
using namespace std;
int main()
{
cout < < 100 < < " ";
cout.setf(ios::hex);
cout < < 100 < < " ";
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < list >
#include < iostream >
using namespace std;
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();
list < int > ::iterator it = l2.begin();
it++; it++;
l1.splice(l1.end(),l2, it, l2.end());
print(l1.begin(), l1.end()); cout < < "Size:" < < l1.size() < < " ";
print(l2.begin(), l2.end()); cout < < "Size:" < < l2.size() < < endl;
return 0;
}
Which method added to class B at the marked spot will allow the code below to compile? Choose all possible solutions.
#include < vector >
#include < iostream >
#include < algorithm >
using namespace std;
class B { int val;
public:
B(int v):val(v){}
int getV() const {return val;}
/* Insert Code Here */
};
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;
}
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < set >
#include < list >
using namespace std;
int main(){
int t[] = { 1, 1, 2, 2, 3, 3, 4, 4, 5, 5 };
list < int > v(t, t+10);
set < int > s1(v.begin(),v.end());
if (s1.count(3) == 2) {
s1.erase(3);
}
for(set < int > ::iterator i=s1.begin();i!= s1.end(); i++) {
cout < < *i < < " ";
}
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 < < " "; } };
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);
merge(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 < vector >
#include < set >
using namespace std;
void myfunction(int i) {
cout < < " " < < i;
}
bool classifier(int v) {
return v%2==0;
}
int main() {
int t[] = { 1, 5, 2, 5, 2, 4, 4, 3, 3, 1 };
vector < int > v1(t, t+10);
set < int > s1(t, t+10);
replace(v1.begin(), v1.end(),classifier, 10);
for_each(v1.begin(), v1.end(), myfunction);
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < iomanip >
using namespace std;
int main ()
{
float f = 10.126;
cout.unsetf(ios::floatfield);
cout < < showpoint < < f < < fixed < < " " < < setprecision(2) < < f < < endl;
return 0;
}
Program outputs:
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() < < endl;
return 0;
}
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=0):val(v){}
int getV() const {return val;}
operator int () 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 < < " "; } };
struct Add {
B operator()(B & a, B & b) { return a+b; } };
int main() {
B t[]={1,2,3,4,5,6,7,8,9,10};
vector < B > v1(t, t+10);
vector < B > v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(Add(),1));
for_each(v2.rbegin(), v2.rend(), 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 < < " ";
}
};
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; }
};
struct Odd { bool operator()(int v) { return v%2==0; }};
int main() {
vector < int > v1(10);
generate(v1.begin(), v1.end(), Sequence(1));
partition(v1.begin(),v1.end(), Odd());
for_each(v1.begin(), v1.end(), Out < int > (cout) );cout < < endl;
return 0;
}
Choose all possible outputs:
What will happen when you attempt to compile and run the code below, assuming that you enter the following sequence: 1 2 3 4 quit < enter > ?
#include < iostream >
#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 ()
{
list < string > l;
while(cin.good())
{
string s;
cin > > s;
if (s == "quit") break;
l.push_back(s);
}
for_each(l.begin(), l.end(), Out < string > (cout));
return 0;
}
Program will output:
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:
What happens when you attempt to compile and run the following code?
#include < list >
#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;
}
Which are NOT valid instantiations of priority_queue object:
#include < iostream >
#include < deque >
#include < list >
#include < queue >
#include < vector >
using namespace std;
int main()
{
deque < int > mydeck; list < int > mylist; vector < int > myvector;
priority_queue < int > first; //line I
priority_queue < int, deque < int > > second; //line II
priority_queue < int > third(first); //line III
priority_queue < int, list < int > > fourth(third); //line IV
priority_queue < int, vector < int > > fifth(myvector.begin(), myvector.end()); //line V
return 0;
}
What happens when you attempt to compile and run the following code?
#include < deque >
#include < iostream >
#include < algorithm >
#include < functional >
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(), greater < B > ());
pair < deque < B > ::iterator, deque < B > ::iterator > result = equal_range(d1.begin(), d1.end(), B(20), greater < B > ());
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 < 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==(const A & b) const { return a == b.a; }
};
bool compare(const A & a, const A & b) { return a == b; }
int main () {
int t[] = {1,2,3,3,5,1,2,4,4,5};
vector < A > v (t,t+10);
vector < A > ::iterator it = v.begin();
while ( (it = adjacent_find (it, v.end(), compare)) != v.end()) {
cout < < it?v.begin() < < " ";it++;
}
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;} };
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 t[]={3,2,4,1,5,6,10,8,7,9};
vector < B > v1(t, t+10);
transform(v1.begin(), v1.end(), v1.begin(), bind2nd(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 < vector >
#include < set >
#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 < < " "; } };
bool Greater(int v1, int v2) { return v1 < v2; }
int main() {
int t[]={8, 10, 5, 1, 4, 6, 2, 7, 9, 3};
vector < int > v1(t, t+10);
sort(v1.begin(), v1.end(), Greater);
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 < 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};
B t2[]={5,6,8,2,1};
vector < B > v1(10,0);
sort(t1, t1+5);
sort(t2, t2+5);
set_intersection(t1,t1+5,t2,t2+5,v1.begin());
for_each(v1.begin(), v1.end(), Out < B > (cout));cout < < endl;
return 0;
}
Program outputs:
What will happen when you attempt to compile and run the code below, assuming you enter the following sequence: 1 2 3 < enter > ?
#include < iostream >
using namespace std;
int main ()
{
int a,b,c;
cin > > a > > b > > c;
cout < < a < < b < < c < < endl;
return 0;
}
Program will output:
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:
What will happen when you attempt to compile and run the following code?
#include < iostream >
#include < set >
#include < vector >
using namespace std;
int main(){
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
vector < int > v(t, t+10);
set < int > s1(v.begin(),v.end());
s1.insert(v.begin(),v.end());
pair < set < int > ::iterator,set < int > ::iterator > range;
range = s1.equal_range(6);
cout < < *range.first < < " " < < *range.second < < endl;
return 0;
}
The output will be:
What happens when you attempt to compile and run the following code?
#include < set >
#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};
set < B > s1(t, t+10);
sort(s1.begin(), s1.end());
for_each(s1.begin(), s1.end(), Out < B > (cout));cout < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < iomanip >
using namespace std;
int main ()
{
float f = 10.126;
cout < < f < < " " < < setprecision(2) < < f < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < vector >
#include < set >
#include < deque >
#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 Sequence {
int start;
Sequence(int start):start(start){}
int operator()() {
return start++ ;
}
};
int main() {
vector < int > v1(5);
generate(v1.begin(), v1.end(), Sequence(1));
set < int > s1(v1.rbegin(), v1.rend());
deque < int > d1(s1.rbegin(), s1.rend());
reverse(v1.begin(),v1.end());
reverse(s1.begin(), s1.end());
reverse(d1.begin(), d1.end());
for_each(v1.begin(), v1.end(), Out < int > (cout) );
for_each(s1.begin(), s1.end(), Out < int > (cout) );
for_each(d1.begin(), d1.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 < vector >
#include < set >
using namespace std;
void myfunction(int i) { cout < < " " < < i;
}
struct sequence {
int val,inc;
sequence(int s, int i):val(s),inc(i){}
int operator()(){
int r = val; val += inc;
return r;
}
};
int main() {
vector < int > v1(10);
fill(v1.begin(), v1.end(), sequence(1,1));
for_each(v1.begin(), v1.end(), myfunction);
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 < < " ";
}
};
struct Sequence {
int start;
Sequence(int start):start(start){}
int operator()() { return start++; } };
int main() {
vector < int > v1(10);
vector < int > v2(10);
generate(v1.begin(), v1.end(), Sequence(1));
random(v1.begin(),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 < 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};
int t1[]={1,2,3,4};
deque < int > d1(t, t+10);
set < int > s1(t, t+10);
sort(d1.begin(), d1.end());
cout < < includes(s1.begin(),s1.end(), t1,t1+4) < < " " < < includes(d1.begin(),d1.end(), t1,t1+4)
< < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < set >
#include < vector >
using namespace std;
int main(){
int t[] = { 3, 4, 2, 1, 6, 5, 7, 9, 8, 0 };
multiset < int > s1(t,t+10);
s1.insert(s1.find(7), 3);
for(multiset < int > ::iterator i=s1.begin();i!= s1.end(); i++) {
cout < < *i < < " ";
}
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;}
B operator +(const B & b )const { return B(val + b.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 < < " "; } };
template < typename A > struct Add : public binary_function < A, A, A > {
A operator() (const A & a, const A & b) const { return a+b; }};
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector < B > v1(t, t+10);
vector < B > v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind1st(ptr_fun (Add < B > ()), 1));
for_each(v2.rbegin(), v2.rend(), Out < B > (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;
}
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;}
B operator +(const B & b )const { return B(val + b.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 < < " "; } };
B Add(B a, B b) { return a+b; }
int main() {
int t[]={1,2,3,4,5,6,7,8,9,10};
vector < B > v1(t, t+10);
vector < B > v2(10);
transform(v1.begin(), v1.end(), v2.begin(), bind2nd(ptr_fun(Add),1));
for_each(v2.rbegin(), v2.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 < deque >
#include < list >
#include < stack >
#include < vector >
using namespace std;
int main()
{
deque < int > mydeck; list < int > mylist; vector < int > myvector;
stack < int > first;
stack < int > second(mydeck);
stack < int > third(second);
stack < int, list < int > > fourth(mylist);
fourth.push(10);fourth.push(11);fourth.push(12);
stack < int, vector < int > > fifth(myvector);
fifth.push(10);fifth.push(11);fifth.push(12);
while(!fifth.empty())
{
cout < < fifth.top() < < " ";
fifth.pop();
}
while (!fourth.empty())
{
cout < < fourth.front() < < " ";
fourth.pop();
}
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;
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(ptr_fun (Add()), 1));
for_each(v2.rbegin(), v2.rend(), Out < int > (cout));cout < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
#include < map >
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 < 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 < 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 < < " "; } };
int main() {
int t[]={3,2,4,1,5,6,10,8,7,9};
vector < int > v1(t, t+10);
for_each(v1.begin(), v1.end(), bind1st(plus < int > (), 1));
for_each(v1.rbegin(), v1.rend(), Out < int > (cout));cout < < endl;
return 0;
}
Program outputs:
What happens when you attempt to compile and run the following code?
#include < iostream >
using namespace std;
int main()
{
cout.setf(ios::oct, ios::basefield);
cout < < 100 < < " ";
cout.setf(ios::showbase);
cout < < 100 < < " ";
return 0;
}
Program outputs: