[go: up one dir, main page]

0% found this document useful (0 votes)
34 views16 pages

Module 7

The document contains 14 code snippets with C++ programs that use various algorithms like transform, for_each, count_if. Each snippet is followed by an output or error description. The summaries analyze the programs and describe their key steps and outputs in 3 sentences or less.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views16 pages

Module 7

The document contains 14 code snippets with C++ programs that use various algorithms like transform, for_each, count_if. Each snippet is followed by an output or error description. The summaries analyze the programs and describe their key steps and outputs in 3 sentences or less.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 16

1, the program outputs 8,20,2,6,4,10,12,

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{
cout << i <<", ";
}

struct MultiAdd:public binary_function <int, int, int>


{
int operator () (const int & _Left, const int & _Right) const
{
return 2* ( _Left + _Right);
}
};

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
deque <int> d1 (mynumbers, mynumbers + 7);
deque <int> d2 (7); //line i
transform (d1.begin (), d1.end (), d2.begin (), bind2nd (MultiAdd () ,
1)); //line ii
for_each (d2.begin (), d2.end (), printer);
return 0;
}

2, the program outputs 3,5,4,1,2,0,9,3

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)

{
cout << i <<", ";
}

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7 );
int counter = count_if (v1.begin (), v1.end (), bind1st (less_equal<int>(),4));
//line i
v1.push_back (counter); //line ii
for_each (v1.rbegin (), v1.rend (), printer); //line ii

return 0;
}

3, complilation error in line ii

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}

};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue ();
return stream;
}

void printer (int i)


{ //line i
cout << i <<", ";
}

template <typename T >struct Add:public binary_function < T, T, T >


{ //line i
T operator () (const T & _Left, const T & _Right) const
{
return _Left + _Right;
}
};

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
vector <Pocket> v2 (7,0);
transform (v1.begin (), v1.end (), v2.begin (), bind1st (ptr_fun (Add <Pocket>
()), 1)); //line ii
for_each (v2.rbegin (), v2.rend (), printer);

return 0;
}

4, the program outputs 9,10,4,7

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{ //line i
cout << i <<", ";
}

int main()
{
int mynumbers1 [] = {3,9,0,2};
int mynumbers2 [] = {6,1,4,5};
vector <int> v1 (4);
transform (mynumbers1, mynumbers1 + 4, mynumbers2, v1.rbegin (), plus <int>
()); //line ii
for_each (v1.rbegin (), v1.rend (), printer);

return 0;
}

5, the program outputs 3,2,1,1,0,1,0

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;
public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)

{
stream << pocket.getValue ();
return stream;
}

void printer (Pocket i)


{ //line i
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
Pocket mynumbers2 [] = {3,8,0,1,0,2,2};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
vector <Pocket> v2 (mynumbers2, mynumbers2 + 7);
vector <Pocket> v3 (7,0);
transform (v1.begin (), v1.end (), v2.begin (), v3.begin (), minus <Pocket>
()); //line ii
for_each (v1.rbegin (), v1.rend (), printer);
return 0;
}

6, unidentified behavior at line III bould be runtime error or 0

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

int Mul (int &_Left)


{
return 2 * _Left;
}

int main()
{
int mynumbers1 [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers1, mynumbers1 + 7);
vector <int> v2 (7);

transform (v1.begin (), v1.end (), v2.begin (), ptr_fun (Mul)); //line i
vector <int>::iterator it = find_if (v2.begin (), v2.end (), bind2nd (equal_to
<int>(), 7)); //line ii
cout << *it <<endl; //line iii
return 0;
}

7, the program outputs 2,-5,1,0,-1,5,-2,

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{
cout << i <<", ";
}

int main()
{
int mynumbers1 [] = {3,9,0,2,1,4,5};
int mynumbers2 [] = {9,0,2,1,4,5,3};
vector <int> v1 (mynumbers1, mynumbers1 + 7);
vector <int> v2 (mynumbers1, mynumbers1 + 7);
vector <int> v3 (mynumbers2, mynumbers2 + 7); //line i
transform (v1.begin (), v1.end (), v2.rbegin (), v3.begin (), minus <int>());
//line ii
for_each (v3.rbegin (), v3.rend (), printer);
return 0;
}

8, , the program outputs 6,5,2,3,1,10,4

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)

{
stream << pocket.getValue ();
return stream;
}

void printer (Pocket i)


{ //line i
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
transform (v1.begin (), v1.end (), v1.begin (), bind2nd (plus <Pocket> (), 1));
//line ii
for_each (v1.rbegin (), v1.rend (), printer);
return 0;
}

9, compilation error in line II

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{
cout << i <<", ";
}

struct Add
{
int operator () (const int & _Left, const int & _Right) const //line i
{
return _Left + _Right;
}
};

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector < int > v1 (mynumbers, mynumbers + 7);
vector < int > v2 (7);
transform (v1.begin (), v1.end (), v2.begin(), bind1st (ptr_fun (Add ()), 1));
//line ii
for_each (v2.begin (), v2.end (), printer);
return 0;
}

10, the program output 7,5,4,1,2,0,9,3,

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)

{
cout << i <<", ";
}

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7 );
int counter = count_if (v1.begin (), v1.end (), bind1st (plus<int>(),4));
//line i
v1.push_back (counter); //line ii
for_each (v1.rbegin (), v1.rend (), printer); //line ii

return 0;
}

11, compilation error in line i

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>
using namespace std;

struct Add:public binary_function <int, int, int>


{
int operator () (int & _Left, const int & _Right) const //line i
{
return _Left + _Right;
}
};

void printer (int i)


{
cout << i <<", ";
}

int main ()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
vector <int> v2 (7);
transform (v1.begin (), v1.end(), v2.begin (), bind1st (Add(), -1)); //line ii
for_each (v2.rbegin (), v2.rend(), printer);

return 0;
}

12, compilation error in line ii

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

struct Add:public binary_function < int, int, int >


{
int operator () (const int & _Left, const int & _Right) const //line i
{
return _Left + _Right;
}
};

void printer (int i)


{
cout << i <<", ";
}

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
vector <int> v2 (7);
transform (v1.begin (), v1.end (), v2.begin (), bind1st (Add , -1)); //line ii
for_each (v2.rbegin (), v2.rend (), printer);
return 0;
}

13, the program outputs 5,10,4,7,0,

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
operator int () const
{
return value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue (); //line i
return stream;
}
void printer (Pocket i)
{
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2};
Pocket mynumbers2 [] = {2,1,4,5};
vector <Pocket> v1 (5,0);
transform (mynumbers1, mynumbers1 + 4, mynumbers2, v1.rbegin (), plus <Pocket>
()); //line ii
for_each (v1.rbegin (), v1.rend (), printer);
return 0;
}
14, the program outputs 5,4,1,2,0,9,3

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}

};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue ();
return stream;
}

void printer (int i)


{ //line i
cout << i <<", ";
}

template <typename T >struct Add:public binary_function < T, T, T >


{ //line i
T operator () (const T & _Left, const T & _Right) const
{
return _Left + _Right;
}
};

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
vector <Pocket> v2 (7,0);
transform (v1.begin (), v1.end (), v2.begin (), bind2nd (Add <Pocket> (),
0)); //line ii
for_each (v2.rbegin (), v2.rend (), printer);

return 0;
}

15, the program outputs 5,4,1,2,0,9,3,

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue ();
return stream;
}
void printer (Pocket i)
{ //line i
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
for_each (v1.begin (), v1.end (), bind1st (plus<Pocket>(),1)); //line ii
for_each (v1.rbegin (), v1.rend (), printer);
return 0;
}

16, the program 6,5,2,3,1,10,4


#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)
{
stream << pocket.getValue ();
return stream;
}
void printer (Pocket i)
{ //line i
cout << i <<", ";
}
struct Add:public binary_function < Pocket, Pocket, Pocket >
{
Pocket operator () (const Pocket & _Left, const Pocket & _Right) const
{
return _Left + _Right;
}
};

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
vector <Pocket> v2 (7,0);
transform (v1.begin (), v1.end (), v2.begin (), bind2nd (Add (), 1)); //line ii
for_each (v2.rbegin (), v2.rend (), printer);
return 0;
}
17, the program outputs 5,4,1,2,0,9,3 (NOT SURE)
try 0,1,0,1,1,2,3 (WRONG)
(3,2,1,1,0,1,0) WRONG

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

class Pocket
{
int value;

public:
Pocket (int value): value (value)
{ }
int getValue ()const
{
return value;
}
operator int () const
{
return value;
}
bool operator < (const Pocket & _Right) const
{
return value < _Right.value;
}
};
ostream & operator << (ostream & stream, const Pocket & pocket)

{
stream << pocket.getValue ();
return stream;
}

void printer (Pocket i)


{ //line i
cout << i <<", ";
}

int main()
{
Pocket mynumbers1 [] = {3,9,0,2,1,4,5};
Pocket mynumbers2 [] = {3,8,0,1,0,2,2};
vector <Pocket> v1 (mynumbers1, mynumbers1 + 7);
vector <Pocket> v2 (mynumbers2, mynumbers2 + 7);
vector <Pocket> v3 (7,0);
transform (v1.begin (), v1.end (), v2.begin (), v3.begin (), minus <Pocket>
()); //line ii
for_each (v1.rbegin (), v1.rend (), printer);
return 0;
}
18, the program outputs 5,4,1,2,0,9,3

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{
cout << i <<", ";
}

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
for_each (v1.begin (), v1.end (), bind2nd (plus <int> (), 1)); //line i
for_each (v1.rbegin (), v1.rend (), printer); //line ii
return 0;
}

19, the program outputs 4,3,0,1,-1,8,2

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

struct Add:public binary_function <int, int, int>


{ //line i
int operator () (const int & _Left, const int & _Right) const
{
return _Left + _Right;
}
};

void printer (int i)


{
cout << i <<", ";
}

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
vector <int> v2 (7);
transform (v1.begin (), v1.end (), v2.begin (), bind1st (Add (), -1)); //line
ii
for_each (v2.rbegin (), v2.rend (), printer);
return 0;
}

20, the program outputs 6

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

int Mul (int &_Left)


{
if (_Left <= 3)
return 2 * _Left;
else
return 6;
}

int main()
{
int mynumbers1 [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers1, mynumbers1 + 7);
vector <int> v2 (7);

transform (v1.begin (), v1.end (), v2.begin (), ptr_fun (Mul)); //line i
vector <int>::iterator it = find_if (v2.begin (), v2.end (), bind2nd (equal_to
<int>(), 6)); //line ii
cout << *it <<endl; //line iii
return 0;
}

19/20!!!!!!!!!

13, compilation error in line ii

#include <iostream>
#include <algorithm>
#include <deque>
#include <set>
#include <vector>
#include <functional>

using namespace std;

void printer (int i)


{
cout << i <<", ";
}

struct Add
{
int operator () (const int & _Left, const int & _Right) const //line i
{
return _Left + _Right;
}
};

int main()
{
int mynumbers [] = {3,9,0,2,1,4,5};
vector <int> v1 (mynumbers, mynumbers + 7);
vector <int> v2 (7);
transform (v1.begin (), v1.end (), v2.begin (), bind1st (ptr_fun (Add () ),
1)); //line ii
for_each (v2.begin (), v2.end (), printer);
return 0;
}

18,

You might also like