Home > Blockchain >  Calling a function with default arguments
Calling a function with default arguments

Time:03-11

void Sort()
{
    vector<int> data;

    string number;
    cin.ignore();
    getline(cin, number);

    data = insertion_sort(number);
    // ASCENDING
    for (int i = 0; i < data.size(); i  )
    {
        cout << data[i] << " ";
    }
    // // DESCENDING
    // for (int i = data.size()-1; i >=0 ; i--)
    // {
    //     cout << data[i] << " ";
    // }
}

int main()
{
    map<string, function<void()>> functions;
    functions["SORT"] = Sort;
    functions["IS_SORTED"] = Is_Sorted;
    functions["GETMAX"] = GetMax;
    functions["APPEND"] = AppendSorted;
    functions["GET_HISTOGRAM"] = GetHistogram;

    int num;
    cin >> num;

    while (num > 0)
    {
        string func;
        cin >> func;
        if (functions.find(func) != functions.end())
        {
            functions[func]();
        }
        cout << endl ;
        num--;
    }

    return 0;
}

I want my Sort() function to be like this: Sort(descending=False). And in the input, users can ask if they want descending or ascending, and if they didn't say, the function should be ascending by default. For example:

input:

2
SORT
1 -2 13 24 -100
SORT DESCENDING
1 2 3 5 6

output:

-100 -2 1 13 24
6 5 3 2 1

CodePudding user response:

You can use default values for parameters in C .

You just have to define the function like:

void sort(bool descending = false) {...}

This way, when you call this function with no parameters, it will use the default value, and when you pass a value, the function will use yours.

Notes:

  • You can't define any default parameter before a non-default parameter.

    void sort(bool descending = true, bool reverse); // Error!!
    
  • You also can't overload this function with another one that has the same non-default parameters (the compiler won't know which one to use).

    void sort(bool descending = false); // OK
    void sort(); // Error!!
    

CodePudding user response:

Your use of std::function<void()> does not allow you to pass any input parameters to Sort() (or the other functions). As such, Sort() (and the other functions) will have to check the user input for extra parameters before parsing data, eg:

void Sort()
{
    vector<int> data;

    string params;
    getline(cin, params);
    params = trim(params); // see: https://stackoverflow.com/questions/216823/

    string numbers;
    getline(cin, numbers);

    data = insertion_sort(numbers);

    if (params == "DESCENDING")
    {
        // DESCENDING
        for (auto it = data.rbegin(); it != data.rend();   it)
        {
            cout << *it << " ";
        }
    }
    else
    {
        // ASCENDING
        for (auto it = data.begin(); it != data.end();   it)
        {
            cout << *it << " ";
        }
    }
}

int main()
{
    map<string, function<void()>> functions;
    functions["SORT"] = Sort;
    functions["IS_SORTED"] = Is_Sorted;
    functions["GETMAX"] = GetMax;
    functions["APPEND"] = AppendSorted;
    functions["GET_HISTOGRAM"] = GetHistogram;

    int num;
    cin >> num;

    while (num > 0)
    {
        string func;
        cin >> func;

        auto it = functions.find(func);
        if (it != functions.end())
        {
            it->second();
        }
        cout << endl;

        --num;
    }

    return 0;
}

Online Demo

Otherwise, the main loop will have to read in input parameters and pass them to the functions, eg:

void Sort(const string &params)
{
    vector<int> data;

    string numbers;
    getline(cin, numbers);

    data = insertion_sort(numbers);

    if (params == "DESCENDING")
    {
        // DESCENDING
        for (auto it = data.rbegin(); it != data.rend();   it)
        {
            cout << *it << " ";
        }
    }
    else
    {
        // ASCENDING
        for (auto it = data.begin(); it != data.end();   it)
        {
            cout << *it << " ";
        }
    }
}

int main()
{
    map<string, function<void(const string&)>> functions;
    functions["SORT"] = Sort;
    functions["IS_SORTED"] = Is_Sorted;
    functions["GETMAX"] = GetMax;
    functions["APPEND"] = AppendSorted;
    functions["GET_HISTOGRAM"] = GetHistogram;

    int num;
    cin >> num;

    while (num > 0)
    {
        string func;
        cin >> func;

        auto it = functions.find(func);
        if (it != functions.end())
        {
            string params;
            getline(cin, params);
            it->second(trim(params));
        }
        else
            cin.ignore(numeric_limits<streamsize>::max(), '\n');

        cout << endl;

        --num;
    }

    return 0;
}

Online Demo

  • Related