Functions


  1. 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;
    }











  1. 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.


  1. 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] << " ";
    }











  1. 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.



  1. 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;
    }











  1. 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.


  1. Which header is need to be used with function objects?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    <functional>



  1. What are the two advantage of function objects than the function call?











  1. 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.