我有一个家庭作业,我需要修改旧作业才能使用模板。
原始赋值要求对动态分配的整数数组进行随机排序,我需要创建一个将其扩展为字符串和双精度的模板。我正在使用while循环和switch-case向用户显示一个菜单,在那里他们可以决定他们使用哪种类型并填充数组。
我试图尽可能减少这种情况,使调试更容易。整数实现没有编译器错误,但是double和string变量没有。
int* pPresortedInts = NULL; //pointer to array of ints
std::cout << std::endl << "How many integers did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedInts = new int[sizeOfArray]; //dynamically allocate array
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cin >> pPresortedInts[counter];
}
//output filled array
std::cout << std::endl << std::endl << "Here are the integers in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cout << std::endl << pPresortedInts[counter];
}
std::cout << std::endl << std::endl;
//shuffle elements and print the result
shuffleSort(pPresortedInts, sizeOfArray);
//Remember to delete dynamically allocated arrays
delete [] pPresortedInts;
pPresortedInts = NULL;
双重实施:
double* pPresortedDoubles = NULL; //pointer to array of doubles
std::cout << std::endl << "How many doubles did you want to sort? ";
std::cin >> sizeOfArray;
pPresortedDoubles = new double[sizeOfArray]; //dynamically allocate array
//prompt user to fill array
std::cout << std::endl << "Great! Please enter " << sizeOfArray << " numbers: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cin >> pPresortedDoubles[counter];
}
//output filled array
std::cout << std::endl << std::endl << "Here are the doubles in their original order: ";
for (int counter = 0; counter < sizeOfArray; counter++) {
std::cout << std::endl << pPresortedDoubles[counter];
}
std::cout << std::endl << std::endl;
//shuffle elements and print the result
shuffleSort(pPresortedDoubles, sizeOfArray);
//Remember to delete dynamically allocated arrays
delete [] pPresortedDoubles;
pPresortedDoubles = NULL;
最后,这是模板本身:
template <class T>
void shuffleSort(T pPresortedArray[], T size) {
T* pShuffledArray = new T[size]; // pointer to dynamically allocated array of a generic type
T randomElement;
T temp;
//fill ShuffledArray with PresortedArray
for (int counter = 0; counter < size; counter++) {
pShuffledArray[counter] = pPresortedArray[counter];
}
for (int counter = 0; counter < size; counter++) {
randomElement = rand()%size; // choose a random element from the ShuffledArray array
//swap that element with the currently selected counter
temp = pShuffledArray[randomElement];
pShuffledArray[randomElement] = pShuffledArray[counter];
pShuffledArray[counter] = temp;
}
std::cout << "Shuffled array: ";
//print the (hopefully) shuffled array
for (int counter = 0; counter < size; counter++) {
std::cout << std::endl << pShuffledArray[counter];
}
std::cout << std::endl << std::endl;
//Delete dynamically allocated array within scope
delete [] pShuffledArray;
pShuffledArray = NULL;
}
我在函数调用时尝试了specifying the type,但是没有修复错误。我不相信我的问题与taking arguments by reference有任何关系。
我倾向于选择Gist来阅读代码,所以我上传了entire program there以及作业说明(以防某些方面有用)。
感谢您的帮助!
答案 0 :(得分:2)
好吧这似乎不对
void shuffleSort(T pPresortedArray[], T size) {
为什么要模拟数组的大小,它总是为size_t
void shuffleSort(T pPresortedArray[], size_t size) {
我怀疑你用'T'全局替换'int': - )
答案 1 :(得分:1)
您的数组大小应为size_t
或int
,但无论如何都不应为T
:)
除此之外,它是一段相当不错的代码。
您可以使用std::swap
来简化它。
此外,您的输入代码也可以很容易地制作成模板,以避免重复。
答案 2 :(得分:0)
在这一行:
void shuffleSort(T pPresortedArray[], T size)
你真的希望参数大小的类型是T。
你可以使用 std :: random_shuffle random_shuffle API 和一个新的API