对各种数据类型输入到数组,并排序输出的问题

时间:2022-09-03 15:11:20
求问那个输入函数怎么改才能适应多种数据类型的数组


#include "stdafx.h"
#include <iostream>
using namespace std;
int size = 0;
int *xVal;
double *yVal;
//排序函数测试应该没什么问题
template <class type>
void sort (type* Array)
{
int tmp = 0;
for (int i = 0; i<size; i++)
{
for (int j = 0; j<size-1; j++)
{
if (Array[j+1] < Array[j])
{
tmp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = tmp;
}
}
}
}
//输入函数,会报错cannot convert from 'int *' to 'int'
//如果不使用参数*p,直接单独在函数加入
//xVal = new type[size];或者yVal = new type[size];
//就不会出问题
template <class type>
void InputElement(type* p)
{
cin >> size;
*p = new type[size];
for (int i = 0; i<size; i++)
{
cout << "请输入第" << i +1 << "个元素:";
cin >> xVal[i];
}
}
//输出函数,应该也没啥问题
template <class type>
void OutputElement(type *pVar,int len)
{
if (*pVar != NULL)
{
for (int i=0; i<len; i++)
{
cout << pVar[i] << " ";
}
cout << endl;
}
}

int main(int argc, char* argv[])
{
cout << "请确定int型数组所含元素个数:";
InputElement<int>(xVal);
cout << "排序前:";
OutputElement<int>(xVal,size);
sort<int>(xVal);
cout << "排序后:";
OutputElement<int>(xVal,size);

cout << "请确定double型数组所含元素个数:";
InputElement<double>(yVal);
cout << "排序前:";
OutputElement<double>(yVal,size);
sort<double>(yVal);
cout << "排序后:";
OutputElement<double>(yVal,size);

return 0;
}

3 个解决方案

#1


这样子的时候执行没问题

// SortArray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
int size = 0;
int *xVal;
double *yVal;

template <class type>
void sort (type* Array)
{
int tmp = 0;
for (int i = 0; i<size; i++)
{
for (int j = 0; j<size-1; j++)
{
if (Array[j+1] < Array[j])
{
tmp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = tmp;
}
}
}
}

template <class type>
void InputElement()
{
cin >> size;
xVal = new type[size];
for (int i = 0; i<size; i++)
{
cout << "请输入第" << i +1 << "个元素:";
cin >> xVal[i];
}
}

template <class type>
void OutputElement(type *pVar,int len)
{
if (*pVar != NULL)
{
for (int i=0; i<len; i++)
{
cout << pVar[i] << " ";
}
cout << endl;
}
}

int main(int argc, char* argv[])
{
cout << "请确定int型数组所含元素个数:";
InputElement<int>();
cout << "排序前:";
OutputElement<int>(xVal,size);
sort<int>(xVal);
cout << "排序后:";
OutputElement<int>(xVal,size);



return 0;
}

#2


//输入函数,会报错cannot convert from 'int *' to 'int' 
*p = new type[size]; 这条语句 改成p=new type[size]; 就好了
而且void InputElement(type* p) 是在栈空间中重新开辟的空间,和xVal的空间是不同的
p = new type[size];并不能让xVal也指向新开辟的空间最好改成引用类型,
void InputElement(type* & p) 
template <class type> 
void InputElement(type* &p) 
{         
cin >> size;     
p = new type[size];     
for (int i = 0; i<size; i++)     
{         
cout << "请输入第" << i +1 << "个元素:";  
       cin >> xVal[i];     

}

本人也正在学C++ 希望以后多多交流~
 

#3


完成了 感谢

#1


这样子的时候执行没问题

// SortArray.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
int size = 0;
int *xVal;
double *yVal;

template <class type>
void sort (type* Array)
{
int tmp = 0;
for (int i = 0; i<size; i++)
{
for (int j = 0; j<size-1; j++)
{
if (Array[j+1] < Array[j])
{
tmp = Array[j];
Array[j] = Array[j+1];
Array[j+1] = tmp;
}
}
}
}

template <class type>
void InputElement()
{
cin >> size;
xVal = new type[size];
for (int i = 0; i<size; i++)
{
cout << "请输入第" << i +1 << "个元素:";
cin >> xVal[i];
}
}

template <class type>
void OutputElement(type *pVar,int len)
{
if (*pVar != NULL)
{
for (int i=0; i<len; i++)
{
cout << pVar[i] << " ";
}
cout << endl;
}
}

int main(int argc, char* argv[])
{
cout << "请确定int型数组所含元素个数:";
InputElement<int>();
cout << "排序前:";
OutputElement<int>(xVal,size);
sort<int>(xVal);
cout << "排序后:";
OutputElement<int>(xVal,size);



return 0;
}

#2


//输入函数,会报错cannot convert from 'int *' to 'int' 
*p = new type[size]; 这条语句 改成p=new type[size]; 就好了
而且void InputElement(type* p) 是在栈空间中重新开辟的空间,和xVal的空间是不同的
p = new type[size];并不能让xVal也指向新开辟的空间最好改成引用类型,
void InputElement(type* & p) 
template <class type> 
void InputElement(type* &p) 
{         
cin >> size;     
p = new type[size];     
for (int i = 0; i<size; i++)     
{         
cout << "请输入第" << i +1 << "个元素:";  
       cin >> xVal[i];     

}

本人也正在学C++ 希望以后多多交流~
 

#3


完成了 感谢