由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - c++ template question:
相关主题
一个C++的概念问题please help debug this code
[菜鸟问题]类模板问题C++ template function一个问题
C++ 菜鸟问一个关于template 的问题。C++中使用back_inserter为啥可以不用#include 和using std::back_inserter;??
STL感觉实在太变态了a c++ question
stl container erase in a looptemplate question
stl的一个问题C++ question about template typedef
iterator一问vector::iterator不对
partial_sort问题请教 C++ std::list iterator 对 template class pointer 的应用问题
相关话题的讨论汇总
话题: template话题: coll话题: int话题: print话题: typename
进入Programming版参与讨论
1 (共1页)
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(),

1 (共1页)
进入Programming版参与讨论
相关主题
请教 C++ std::list iterator 对 template class pointer 的应用问题stl container erase in a loop
template 疑问stl的一个问题
请教一下这个template function在gcc下要怎么修改iterator一问
templatepartial_sort问题
一个C++的概念问题please help debug this code
[菜鸟问题]类模板问题C++ template function一个问题
C++ 菜鸟问一个关于template 的问题。C++中使用back_inserter为啥可以不用#include 和using std::back_inserter;??
STL感觉实在太变态了a c++ question
相关话题的讨论汇总
话题: template话题: coll话题: int话题: print话题: typename