【问题标题】:Using Template Function for int, float and char for finding the lowest value使用 int、float 和 char 的模板函数来查找最小值
【发布时间】:2014-11-25 04:45:09
【问题描述】:

提示: 将函数 theSmallest() 和 fill_array() 转换为模板函数。 使用 3 种不同的数据类型测试您的模板函数:int、float 和 char。

问题: 当我将它加载到编译器中时,没有错误。它适用于 INT 和 Float;但是,当我为字符选项运行 index_of_smallest 函数时......它不喜欢它。当我在 char 调用期间用“cout”实际检查 min 的值时,它给了我“-10002e89..”。该值永远不会改变,因此在 for 循环中 index_of_min 始终为 0,因此编译器认为最小字符始终位于索引 0 处。

代码:

#include <iostream>
#include <cstdlib>
using namespace std;
const int DECLARED_SIZE = 20;

template <class BaseType>
int index_of_smallest(BaseType a[], int size, int& number_used)
{
    BaseType min = a[0];
    int index_of_min = 0;
    cout << index_of_min<< endl;
    for (int index = 1 ; index < number_used; index++)
    {
        if (a[index] < min)
        {
            min = a[index];
            index_of_min = index;
        }
    }
    cout << index_of_min<< endl;
    return  index_of_min;
}

template <class BaseType>
void fill_array(BaseType a[], int size, int& number_used)
{ 
    int index = 0;
    BaseType ans;

    cout << "Enter up to " << size << " of the Data Type selected.\n"
         << "Mark the end of a list of numbers with a negative number.\n"
         << "Mark the end of a list of characters with the negative symbol \"-\". \n" 
         << "Separate each input with a space. \n" << endl;
    cin >> ans;

    while (ans >= 0 && ans != '-')
    {
        a[index] = ans;
        index++;
        cin >> ans;
    }
    number_used = index;
}

void menu()
{
    int number_used, choice, smallest, result;
    int start_index = 0;
    char menuLoop = 'Y';

    while (toupper(menuLoop) == 'Y')
    {
        cout << "Please select a type of Input: \n"
             << "1. Integers (1, 2, 3, 4, etc..)\n"
             << "2. Decimals (1.0, 2.0, 3.0. 4.0, etc..)\n"
             << "3. Characters ( a, b, c, d, etc...)\n\n\n";
        cin >> choice;


        switch (choice)
        {
            case 1:
                int arrI[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrI, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrI, start_index, number_used);
                cout << "The smallest integer is: " << arrI[smallest] << " ";
                break;
            case 2:
                float arrF[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrF, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrF, start_index, number_used);
                cout << "The smallest float is: "<< arrF[smallest] << " ";
                break;
            case 3:
                char arrC[DECLARED_SIZE];
                system ("CLS");
                fill_array(arrC, DECLARED_SIZE, number_used);
                smallest = index_of_smallest(arrF, start_index, result);
                cout << "The smallest character is: " << arrC[smallest] <<" \n";
                break;
            default:
                cout << "That is an invalid choice! Exiting Program";
        }

        cout << "\n\nWould you like to return to the main menu? (Y/N): ";
        cin >> menuLoop;

        while(toupper(menuLoop) != 'Y' && toupper(menuLoop) != 'N')
        {
            cout << "Invalid Choice: Please type (Y/N): ";
            cin >> menuLoop;
        }

        system("CLS");
    }
}

int main()
{
    using namespace std;

    menu();
    system("PAUSE");
}

【问题讨论】:

  • 您在case 3 通话中使用了arrF。你应该使用arrC
  • [OT]:注意ans != '-':如果你输入int 45(等于'-'),你可能会遇到奇怪的行为。

标签: c++ function sorting templates


【解决方案1】:

你的错误在这里:

smallest = index_of_smallest(arrF, start_index, result);

应该是:

smallest = index_of_smallest(arrC, start_index, number_used);

您使用了错误的数组,并且您传递了result(从未初始化)而不是number_used

【讨论】:

    猜你喜欢
    • 2012-03-22
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2013-09-28
    • 1970-01-01
    • 2016-08-12
    相关资源
    最近更新 更多