Functions
- What is the output of this program?
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main ()
{
vector <string*> num;
num.push_back ( new string ("First") );
num.push_back ( new string ("Second") );
num.push_back ( new string ("Third") );
num.push_back ( new string ("Fourth") );
num.push_back ( new string ("Fifth") );
vector <int> lengths ( num.size() );
transform (num.begin(), num.end(), lengths.begin(),
mem_fun(&string :: length));
for (int k = 0; k < 5; k++)
{
cout << lengths[k] << " ";
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We calculated the number of letters in every string by using function objects.
- What is the output of this program?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int num[] = {-12, -41, -15, -20, -10};
transform ( num, num + 2, num, negate<int>() );
for (int k = 0; k < 5; ++k)
cout << num[k] << " ";
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: E
In this program, We negated the given value by using the functional objects, So it is producing this output.
- What is the output of this program?
#include <iostream>
#include <functional>
#include <algorithm>
using namespace std;
int main ()
{
int FirstData[] = {23, 33, 43, 52, 56};
int SecondData[] = {2, 3, 4};
int Res[6];
transform ( FirstData, FirstData + 6, SecondData, Res, divides<int>());
for (int k = 0; k < 5; k++)
{
cout << Res[k] << " ";
}
return 0;
}
-
View Hint View Answer Discuss in Forum
NA
Correct Option: A
In this program, We are dividing the first with the second by using function objects.
- Which header is need to be used with function objects?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: C
<functional>
- What are the two advantage of function objects than the function call?
-
View Hint View Answer Discuss in Forum
NA
Correct Option: D
A function object can contain state. The second is that a function object is a type and therefore can be used as a template parameter.