由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ template Questions (转载)
相关主题
这两个地方是否需要typename?function pointer 和 call-back function 有什么区别?
请教一下这个template function在gcc下要怎么修改a c++ question
c++ template question:请教一个boost::bind的问题
template 疑问这段 C++ 怎么改才能编译?
呼唤大侠们,我实在不能实现C++泛型的精神。about STL functor and function pointers
stl 的 member type 看起来挺头大的C++ template
C++ 菜鸟问一个关于template 的问题。今天没有上班,说一个FEATURE吧,concept lite
C++ 程序求助access function static variable
相关话题的讨论汇总
话题: container话题: reduce话题: function话题: typename话题: iter
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
【 以下文字转载自 JobHunting 讨论区 】
发信人: qqcheerup (qq), 信区: JobHunting
标 题: C++ template Questions
发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
有1道题请教大家
1. 实现 Reduce function using templates. The Reduce fn applies a function
of two arguments cumulatively to the items of an STL container, from
begin() to end(), so as to reduce the sequence to a single value. For
example, Reduce(, std::plus()) should
calculate ((((1+2)+3)+4)+5).
class NotEnoughElements {};
template
typename Container::value_type
Reduce(const Container& c, Function fn) throw (NotEnoughElements)
{
实现

}
t****t
发帖数: 6806
2
and your question is?

【在 g*********s 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: qqcheerup (qq), 信区: JobHunting
: 标 题: C++ template Questions
: 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
: 有1道题请教大家
: 1. 实现 Reduce function using templates. The Reduce fn applies a function
: of two arguments cumulatively to the items of an STL container, from
: begin() to end(), so as to reduce the sequence to a single value. For
: example, Reduce(, std::plus()) should
: calculate ((((1+2)+3)+4)+5).

S*********g
发帖数: 5298
3
这个好像是interactive brokers的面试题。
我过了他们的这个written test来着。
1000伪币,我把我的答案卖给你。哈哈哈哈

【在 g*********s 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: qqcheerup (qq), 信区: JobHunting
: 标 题: C++ template Questions
: 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
: 有1道题请教大家
: 1. 实现 Reduce function using templates. The Reduce fn applies a function
: of two arguments cumulatively to the items of an STL container, from
: begin() to end(), so as to reduce the sequence to a single value. For
: example, Reduce(, std::plus()) should
: calculate ((((1+2)+3)+4)+5).

r*******m
发帖数: 109
4
template
typename Container::value_type
Reduce(const Container& c, Function fn)
{
if (c.size()<1) throw NotEnoughElement();
typename Container::value_type T(0);
for (typename Container::const_iterator it=c.begin();it!=c.end();++it)
T=fn(T,*it);
return T;
}

【在 g*********s 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: qqcheerup (qq), 信区: JobHunting
: 标 题: C++ template Questions
: 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
: 有1道题请教大家
: 1. 实现 Reduce function using templates. The Reduce fn applies a function
: of two arguments cumulatively to the items of an STL container, from
: begin() to end(), so as to reduce the sequence to a single value. For
: example, Reduce(, std::plus()) should
: calculate ((((1+2)+3)+4)+5).

q*******p
发帖数: 39
5
Thanks
but how do you known function has two parameters?
r*******m
发帖数: 109
6
that's in the original question. binary functor. If you need to check input
function, thats more complicated since it's compiler time issue. Given
unirory functor won't work in compiler time.

【在 q*******p 的大作中提到】
: Thanks
: but how do you known function has two parameters?

g*********s
发帖数: 1782
7
what's the best answer.

【在 t****t 的大作中提到】
: and your question is?
g*********s
发帖数: 1782
8
what if there's no "zero" defined for type T?

it=c.begin();it!=c.end();++it)

【在 r*******m 的大作中提到】
: template
: typename Container::value_type
: Reduce(const Container& c, Function fn)
: {
: if (c.size()<1) throw NotEnoughElement();
: typename Container::value_type T(0);
: for (typename Container::const_iterator it=c.begin();it!=c.end();++it)
: T=fn(T,*it);
: return T;
: }

t****t
发帖数: 6806
9
you are not supposed to use 0 as initial value, as the question states you
are supposed to reduce(1, 2, 3, 4, 5) to ((((1, 2), 3), 4), 5), as opposed
to (((((0, 1), 2), 3), 4), 5)

【在 r*******m 的大作中提到】
: template
: typename Container::value_type
: Reduce(const Container& c, Function fn)
: {
: if (c.size()<1) throw NotEnoughElement();
: typename Container::value_type T(0);
: for (typename Container::const_iterator it=c.begin();it!=c.end();++it)
: T=fn(T,*it);
: return T;
: }

r*******m
发帖数: 109
10
as someone said, you can start it+1, initialize the T with the first element
. small change of code.

【在 g*********s 的大作中提到】
: what if there's no "zero" defined for type T?
:
: it=c.begin();it!=c.end();++it)

相关主题
stl 的 member type 看起来挺头大的function pointer 和 call-back function 有什么区别?
C++ 菜鸟问一个关于template 的问题。a c++ question
C++ 程序求助请教一个boost::bind的问题
进入Programming版参与讨论
e********e
发帖数: 250
11
Meyer said "avoid using size(), use empty()".
g*********s
发帖数: 1782
12
oh, yes. when func is *, reduction from 0 actually is wrong.

you
opposed

【在 t****t 的大作中提到】
: you are not supposed to use 0 as initial value, as the question states you
: are supposed to reduce(1, 2, 3, 4, 5) to ((((1, 2), 3), 4), 5), as opposed
: to (((((0, 1), 2), 3), 4), 5)

g*********s
发帖数: 1782
13
can't use empty() bah. should be:
if ( size() < 2) throw NotEnoughElements.

【在 e********e 的大作中提到】
: Meyer said "avoid using size(), use empty()".
e********e
发帖数: 250
14
1 element is okay, it is the result. even if one element is not allowed,
avoid using size() as it is slow for list, do things like it = c.begin(), it
!= c.end() && ++it != c.end().

【在 g*********s 的大作中提到】
: can't use empty() bah. should be:
: if ( size() < 2) throw NotEnoughElements.

e****d
发帖数: 895
15
function body()
{
if (c.begin() == c.end())
throw NotEnoughElements();
Container::const_iterator iter = c.begin();
return std::accumulate(iter, c.end(), *iter++, fn);
}

【在 g*********s 的大作中提到】
: 【 以下文字转载自 JobHunting 讨论区 】
: 发信人: qqcheerup (qq), 信区: JobHunting
: 标 题: C++ template Questions
: 发信站: BBS 未名空间站 (Sun Jan 16 22:00:26 2011, 美东)
: 有1道题请教大家
: 1. 实现 Reduce function using templates. The Reduce fn applies a function
: of two arguments cumulatively to the items of an STL container, from
: begin() to end(), so as to reduce the sequence to a single value. For
: example, Reduce(, std::plus()) should
: calculate ((((1+2)+3)+4)+5).

t****t
发帖数: 6806
16
this looks good but unfortunately is undefined. you can't use iter and *iter
++ like this in one expression.

【在 e****d 的大作中提到】
: function body()
: {
: if (c.begin() == c.end())
: throw NotEnoughElements();
: Container::const_iterator iter = c.begin();
: return std::accumulate(iter, c.end(), *iter++, fn);
: }

e****d
发帖数: 895
17
Shouldn't parameters are evaluated (even though the order is undefined)
before the function body is called including copying parameters?

iter

【在 t****t 的大作中提到】
: this looks good but unfortunately is undefined. you can't use iter and *iter
: ++ like this in one expression.

e****d
发帖数: 895
18
Oh, actually, it's true the undefined order causes undefined values
are passed in.

【在 e****d 的大作中提到】
: Shouldn't parameters are evaluated (even though the order is undefined)
: before the function body is called including copying parameters?
:
: iter

t****t
发帖数: 6806
19
it's guaranteed that parameters are evaluated before function. however, as i
said, iter and *iter++ can not be evaluated simutaneously.

【在 e****d 的大作中提到】
: Shouldn't parameters are evaluated (even though the order is undefined)
: before the function body is called including copying parameters?
:
: iter

t****t
发帖数: 6806
20
no no, there is no such concept as "undefined values" in c++. there is
undefined behaviour, and there is indetermined value.

【在 e****d 的大作中提到】
: Oh, actually, it's true the undefined order causes undefined values
: are passed in.

e****d
发帖数: 895
21
That's right. Should be "indetermined value", which
causes "undefined behaviour".

【在 t****t 的大作中提到】
: no no, there is no such concept as "undefined values" in c++. there is
: undefined behaviour, and there is indetermined value.

1 (共1页)
进入Programming版参与讨论
相关主题
access function static variable呼唤大侠们,我实在不能实现C++泛型的精神。
Haskell很难学。。stl 的 member type 看起来挺头大的
STL感觉实在太变态了C++ 菜鸟问一个关于template 的问题。
[菜鸟问题]类模板问题C++ 程序求助
这两个地方是否需要typename?function pointer 和 call-back function 有什么区别?
请教一下这个template function在gcc下要怎么修改a c++ question
c++ template question:请教一个boost::bind的问题
template 疑问这段 C++ 怎么改才能编译?
相关话题的讨论汇总
话题: container话题: reduce话题: function话题: typename话题: iter