F*******i 发帖数: 190 | 1 I am trying to test a template function to print out all
the elements of stl containers:
template class T > void print( const T
> & coll )
{
copy (coll.begin(), coll.end(), // source
ostream_iterator(cout," ")); // destination
cout << endl;
}
list coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
print >(coll); / | t****t 发帖数: 6806 | 2 //print >(coll);
change to
print (coll);
or
print(coll);
or avoid template template parameter at all:
template
void print(const V& coll)
{
copy (coll.begin(), coll.end(),
ostream_iterator(cout, " "));
}
【在 F*******i 的大作中提到】 : I am trying to test a template function to print out all : the elements of stl containers: : template class T > void print( const T: > & coll ) : { : copy (coll.begin(), coll.end(), // source : ostream_iterator(cout," ")); // destination : cout << endl; : } : list coll;
| h****e 发帖数: 2125 | 3 too intrusive, what if V is not a standard type and doesn't have value_type?
【在 t****t 的大作中提到】 : //print >(coll); : change to : print (coll); : or : print(coll); : or avoid template template parameter at all: : template : void print(const V& coll) : { : copy (coll.begin(), coll.end(),
| t****t 发帖数: 6806 | 4 intrusive ahead. you have to assume something -- what if the container doesn
't have begin(), end()?
if you can assume begin()/end(), you can also assume ::value_type. otherwise
, just accept 2 iterators instead, like what real STL do.
type?
【在 h****e 的大作中提到】 : too intrusive, what if V is not a standard type and doesn't have value_type?
| c*****t 发帖数: 1879 | 5 agree. That is the reason it is called template programming.
doesn
otherwise
【在 t****t 的大作中提到】 : intrusive ahead. you have to assume something -- what if the container doesn : 't have begin(), end()? : if you can assume begin()/end(), you can also assume ::value_type. otherwise : , just accept 2 iterators instead, like what real STL do. : : type?
| h****e 发帖数: 2125 | 6 his function is intrusive, your solution is even more intrusive, that's it.
doesn
otherwise
【在 t****t 的大作中提到】 : intrusive ahead. you have to assume something -- what if the container doesn : 't have begin(), end()? : if you can assume begin()/end(), you can also assume ::value_type. otherwise : , just accept 2 iterators instead, like what real STL do. : : type?
| h****e 发帖数: 2125 | 7 what reason?
【在 c*****t 的大作中提到】 : agree. That is the reason it is called template programming. : : doesn : otherwise
| t****t 发帖数: 6806 | 8 his function is under condition "STL container". it's not the best generic
function, but given this condition, it's perfectly fine. maybe you should
read other ppl's requirement before you comment.
【在 h****e 的大作中提到】 : his function is intrusive, your solution is even more intrusive, that's it. : : doesn : otherwise
| F*******i 发帖数: 190 | 9 Thanks all, I think the other way to go is using iterator of stl,
【在 t****t 的大作中提到】 : //print >(coll); : change to : print (coll); : or : print(coll); : or avoid template template parameter at all: : template : void print(const V& coll) : { : copy (coll.begin(), coll.end(),
|
|