Join 118,871 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,781 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!
hopefully i put this in the right place i was not sure if it was in visual basics or in the c++ catergory. but if this is the wrong place let me know so i can start to post in the right place.okay im working on a sorting array. that has some numbers and correctly arranges them in order using an int array. i use a selection sort and a swap function to do this. my problem is i now have to make a function template called selection sort that does the same thing. i have read online about templates but still find them confusing. even though the idea of them seem easy. how do i make it into a function template selectionsort?this is my code:
CODE
#include<iostream> #include<iomanip> using namespace std;
void selectionSort(int*const array, const int size){ int smallest; //loop over size-1 elements for(int i=0;i<size-1;i++){ smallest=i; //loop to find index of smallest element for(int index=i+1;index<size;index++) if(array[index]<array[smallest]) smallest=index; swap(&array[i],&array[smallest]); } } int main( ){
const int arraySize=10; int a[arraySize]={2,6,4,8,10,12,89,68,45,37};
Hey, I am not very good at this either, but I was able to take your existing code and modify it enough to work. I was able to use some of the stuff off of another Array I had done before and place it in yours and I was able to make it compile. Take a look at it, I sure hope this helps you out, I am pretty much a beginner at this, but I have been messing around with the Arrays for a couple of my last projects. Well good luck and let us know if this works for you.
CODE
#include<iostream> #include<iomanip> using namespace std;
void selectionSort(int*const array, const int size){ int smallest; //loop over size-1 elements for(int i=0;i<size-1;i++){ smallest=i; //loop to find index of smallest element for(int index=i+1;index<size;index++) if(array[index]<array[smallest]) smallest=index; swap(&array[i],&array[smallest]); } }
#define SIZE 12 int main() { int b, c, d, e, f, g, h, j, k, l, m, p;
cout << "We will enter 12 positive numbers : ";
cout << "Enter the first number : "; cin >> b; cout << "Enter the second number : "; cin >> c; cout << "Enter the third number : "; cin >> d; cout << "Enter the fourth number : "; cin >> e; cout << "Enter the fifth number : "; cin >> f; cout << "Enter the sixth number : "; cin >> g; cout << "Enter the seventh number : "; cin >> h; cout << "Enter the eighth number : "; cin >> j; cout << "Enter the ninth number : "; cin >> k; cout << "Enter the tenth number : "; cin >> l; cout << "Enter the eleventh number : "; cin >> m; cout << "Enter the twelvth number : "; cin >> p;
int a[SIZE] = {b, c, d, e, f, g, h, j, k, l, m, p};
well the main thing i want to do is make a Template like this: template<typename T> and implement it into my selection sort. im having trouble making it into a template.
well the main thing i want to do is make a Template like this: template<typename T> and implement it into my selection sort. im having trouble making it into a template.
hey shadow....in case you are still having problems here is a suggestion.
CODE
typedef void SPELL_FUN args( ( int sn, int level, CHAR_DATA *ch, void *vo, int target ) );
this basically creates a template of the function input and associates it with the name SPELL_FUN
then when you want to use the function declare a variable with the name SPELL_FUN and it will automatically assume the value typedef'd above
as such in usage:
CODE
... typedef void SPELL_FUN args( ( int sn, int level, CHAR_DATA *ch, void *vo, int target ) ); //declaration ... SPELL_FUN * spell_fun; //usage ...
would translate to:
CODE
... void spell_fun (int sn, int level, CHAR_DATA *ch, void *vo, int target) ..
you may ask your self why not just do the bottom one instead of the top one. Think of as a template and cuts the code typing down and still makes the same reference.
as well it allows you to declare functions just like variables and of course with less typing.
How to make a function template. Well, lets start with the swap since that is a classic and can be found within C++'s algorithm header (well I could not find it but it should be there somewhere).
Generally you will see this looking something like this:
cpp
template <class T> void swap(T &first, T &second) { T temp; temp = first; first = second; second = temp; }
So converting you current function over into template functions is not all that hard, you more or less will prefix them with "template <class T>" and then replace "int" with "T"... There may be a little more to it than that, but that is the basics.
I am betting that PsychoCoder beats me to answer this. And this should be a question should be in the VB.net forum (I am not a .net programmer).
However I believe that what you would be interested in is "Generics" in VB.net
This is not the same as templates (templates are MUCH cooler than generics).
In good old VB (non.net) you would use late binding to simulate the generic. You can use the same technique in VB.net using the Object datatype. However I *think* that VB.net has the ability to use Generics more formally than late binding.