 |
|
 |
Not bad! I just thought I would give you some minor observations to consider.
A couple of things:
1. Since iTop and iSize represent integer values (iTop >= iSize) and (iTop <= 0) is much safer than using == for comparison.
2. You are using iSize in your code, but that was given to the template as an instantiation parameter and should no longer exist after a Stack variable has been created. Therefore you need to have an internal copy stored in the Stack class.
3. In an updated or future article, you may mention that the argument to a function template is ‘implied’ by the argument that is passed. That is why we do not have to 'explicitly' call it like this: calcim<int>(iField,iSize);.
INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
Today I mean replace template < class ElemType > with template < typename ElemType > is more according recommendation.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
As some people posted the examples I published above don't compile 'cause of some typo errors... Sorry for that, I'll do better in Part II.
Anyway, I'd like to correct the stuff, but the 'Edit article' link at the top is lost. Anyone knows about how to get the link back?
Regards
Stefan
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Only minor changes. I'll do it this weekend (I'll try, as I'm totally busy with Warcraft III )
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
 | Welcome  Thomas Freudenberg | 0:03 18 Jul '02 |
|
 |
Hi Stefan,
nice to see you have written your first article at CP. Why did you never use templates when you were employed here?
Regards Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Nishant S wrote: Was Stefan your colleague once, Thomas?
Yes, he left us several month ago. Seems as he learns much more in his new company
Regards Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thomas Freudenberg wrote: Seems as he learns much more in his new company
Maybe that is why he left 
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Regards Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Christian Graus wrote: Maybe that is why he left
Actually, the main reason was Hamburg, IMHO best place to live in Germany
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Thomas Freudenberg wrote: Why did you never use templates when you were employed here?
I thought about using it when I was doing my ICC module stuff, but wasn't necessary in my code
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Stefan Spenz wrote: I was doing my ICC module stuff, but wasn't necessary in my code
..but a .cpp file about 328 KB with 11.000 lines?
Regards Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thomas Freudenberg wrote: but a .cpp file about 328 KB with 11.000 lines
Sure, but templates were not neccesary there. You might say I could have splitted up my code, but I wanted to create a nice file so that you guys @ Cycos always remember me
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Stefan Spenz wrote: You might say I could have splitted up my code, but I wanted to create a nice file so that you guys @ Cycos always remember me
Fortunately, it's RED's task to maintain your code
Regards Thomas
Sonork id: 100.10453 Thömmi
Disclaimer: Because of heavy processing requirements, we are currently using some of your unused brain capacity for backup processing. Please ignore any hallucinations, voices or unusual dreams you may experience. Please avoid concentration-intensive tasks until further notice. Thank you.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
A nice gentle introduction to templates. A suggestion to people who are just starting out with templates is to really understand the concept of iterators.
For example, considering the example that finds the minimum element of an array. An iterator based version could look something like this:
template <class Iterator> Iterator min(Iterator begin, Iterator end) { Iterator mini = begin;
if(begin != end) while(++begin != end) if(*begin < *mini) mini = begin;
return mini; }
Here the templated type is the iterator. Instead of calling the function with a pointer to an array and a size we call the function with two iterators. An iterator is an abstraction of a pointer and the neat thing is that a regular pointer is an iterator.
int i = *min(iField, iField + (sizeof(iField) / sizeof(int)));
The advantage of this approach is that it will now work with regular arrays, std::list, std::vector etc. It'll work with any container that can supply you with start and end iterators (like the containers in STL).
The STL is packed with containers and algorithms and it's the iterators that binds it all together. Incidentally there's a template function in the standard library that does exactly what the above function does: std::min_element
Anyway, nice article. Keep up the good work.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Daniel Andersson wrote: A nice gentle introduction to templates.
That's exactly what Part I should be...
Daniel Andersson wrote: Anyway, nice article. Keep up the good work.
thx!
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Daniel Andersson wrote: An iterator is an abstraction of a pointer and the neat thing is that a regular pointer is an iterator.
you say the opposite into the same sentence... can you please explain a little because i'm confused. thx
TOXCCT >>> GEII power
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
What I meant was that the iterator concept is an abstraction of pointers.
Iterators in the standard library are implemented using an interface that mimics that of regular pointers. Iterators are operated on using operator++, operator--, operator* etc. Just like a regular pointer an iterator is dereferenced using *. This makes it possible to use pointers in functions that take iterators:
// call std::sort with vector iterators std::vector<int> vec(100); std::sort(vec.begin(), vec.end());
// call std::sort with pointers int array[100]; std::sort(array, array + 100);
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Can I suggest you not use the word 'vector' to denote an array ? Also, you seem to have lost the <int> from the 'min' example, it looks like you're using the min macro instead.
I'm kind of spewing that I did not realise there were not template tutorials on CP. I look forward to reading more, especially about template specialisation, and traits.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Christian Graus wrote: Can I suggest you not use the word 'vector' to denote an array
Did I write 'vector'? Shame, shame...
When I was young my mother told me to clean up my room, I told her -1!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Stefan Spenz wrote: Did I write 'vector'?
Yes, I was totally confused at first. Then I thought you were calling the min MACRO and calling it a function, but I worked it all out in the end.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Christian Graus wrote: Also, you seem to have lost the <int> from the 'min' example
No! He hasn't missed it! It's not required. When the compiler encounters any instantiation of a function template, it'll generate the matching function!
Regards, Nish
p.s. I didn't know this of course, but after seeing your comment, I checked up elsewhere!
Author of the romantic comedy
Summer Love and Some more Cricket [New Win]
Review by Shog9 Click here for review[NW]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If that's the case, how does the compiler decide he's not calling the min macro ? The preprocessor needs to figure that out.
Christian
come on all you MS suckups, defend your sugar-daddy now. - Chris Losinger - 11/07/2002
|
| Sign In·View Thread·PermaLink | 4.00/5 |
|
|
|
 |