Jul 16, 2016 at 5:37pm UTC
using namespace std;
template<class T>
void Bubblesort() {
T *unsorted, swap;
unsorted = new T[999];
srand(time(NULL));
cout << "1" << endl;
for (int i = 0; i < 999; i++) {
unsorted[i] = rand() % 1000;
cout << unsorted[i] << endl;
}
int start_s = clock();
for (int i = 0; i < 999; i++)
{
for (int j = 0; j < 998; j++)
{
if (unsorted[j] > unsorted[j + 1])
{
swap = unsorted[j];
unsorted[j] = unsorted[j + 1];
unsorted[j + 1] = swap;
}
}
}
int stop_s = clock();
cout << "time taken: " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << endl;
for (int i = 0; i < 999; i++) {
cout << unsorted[i] << endl;
}
};
int main() {
void Bubblesort();
system("pause");
return 0;
}
Jul 16, 2016 at 6:06pm UTC
From what Thomas1974 said :
==>
1 2 3 4 5 6
int main()
{
void Bubblesort<???>();
system("pause" );
return 0;
}
Last edited on Jul 16, 2016 at 6:16pm UTC
Jul 16, 2016 at 6:15pm UTC
1 2
template <class T>
void Bubblesort(){}
This template function has one template parameter <class T>, meaning it requires at least one argument. Simply calling
Bubblesort()
will not work. You must supply the <class T> in order to be able to call this function.
For example :
1 2
Bubblesort<int >();
Bubblesort<double >();
Last edited on Jul 16, 2016 at 6:17pm UTC