avatar
c++ template question:# Programming - 葵花宝典
F*i
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); /
avatar
t*t
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;

avatar
h*e
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(),

avatar
t*t
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?
avatar
c*t
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?

avatar
h*e
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?

avatar
h*e
7
what reason?

【在 c*****t 的大作中提到】
: agree. That is the reason it is called template programming.
:
: doesn
: otherwise

avatar
t*t
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

avatar
F*i
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(),

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。