0
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
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Dr. Erfox
  • 9
  • 2
  • 1
    Yes, fine, and did you try `void Sort(bool descending=false)`? – ypnos Mar 10 '22 at 22:33
  • Then what?...How should i get descending or ascending in this ''' functions["SORT"] = Sort;'''part? – Dr. Erfox Mar 10 '22 at 22:39
  • Have you tried using a class? – Taekahn Mar 10 '22 at 22:43
  • You can't get that into functions["SORT"] unless you put it into all functions. You should probably split the command at the first " " and pass the remainder as option to the functions. Each function can then decide if it can use the argument or give an error. PS: you can make a wrapper function to reject extra args and use that on all but sort. – Goswin von Brederlow Mar 11 '22 at 00:45

2 Answers2

1

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!!
    
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
KyreX
  • 106
  • 5
0

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

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770