本文介绍了实现库算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在从Accelerated C ++中进行练习8-2。我们应该在哪里实现库算法,试图最小化迭代器的数量 操作。到目前为止我有两个问题。 首先,我花了一段时间才弄清楚如何正确编译 : // begin和begin2必须都是输入迭代器。 // template< class In,class Out> out accumulate(在begin,const in end,Out) { Out ret = 0; + * * *开始++; 返回ret; > } 问题是我不理解函数的签名。当我们定义一个函数时,每个参数都需要一个类型和一个参数名称。所以这个 对我来说很有意义: template< class In,class Out> out accumulate(在开头,const在最后,Out t) { ... } 但不是这样: 模板< class In,class Out> out accumulate(在开头,const在最后,Out) { ... } 当然,对我来说有意义的那个没有编译。编译器只需要需要参数的类型;实际价值是无关紧要的: int i = accumulate(v.begin(),v.end(),0); 但是定义一个带有类型的参数的函数仍然感觉很奇怪, 但是没有名字。有没有办法更好地理解为什么语言的语法 是这样的? 其次,我遇到了麻烦with transform()。 template< class In> bool transform(在b中,const在e中,在dest中,在(* f)(In in)); template< class in> in mySquare(in arg); int main(void) { vector< int> v1,v2; for(vector< int> :: size_type i = 1; i< = 10; ++ i) v1.push_back (i); transform(v1.begin(),v1.end(),back_inserter(v2),mySquare); 返回0; } 模板< class in> in mySquare(in arg) { return arg * arg; } // begin和begin2必须都是输入迭代器。 // template< class In> bool transform(在开头,const在最后,在dest,In(* f)(In in)) { while(开始!=结束) * dest ++ = f(*开始++); 返回dest; } 编译器找不到任何符合我对transform(), 的调用的函数,所以我把它吹到了某个地方。如何修复此代码? 谢谢, 彼得 解决方案 * Pete:我正在从Accelerated C ++中进行练习8-2。我们应该在哪里实现库算法,尽量减少迭代器操作的数量。 如果该练习与您的问题相关,您应该引用 。 我有两个问题到目前为止。 首先,我花了一段时间才弄清楚如何正确编译积累() // begin和begin2必须同时输入迭代器。 // 模板< class In,class Out> Out accumulate(在开头,const在最后,Out) { Out ret = 0; while(begin!=结束) ret + = *开始++; 返回ret; } 对于std :: accumulate,第三个参数提供了一个 的初始值你称之为''ret''。 问题是我不理解函数的签名。当我们定义一个函数时,每个参数都需要一个类型和一个参数名称。 不,它只需要一种类型。 所以这对我来说很有意义: 模板< class In,class Out> Out accumulate(在开头,const在最后,Out t) { ... } 但是不是这个: 模板< class In,class Out> Out accumulate(在开头,const在最后,Out) { ... } 当然,对我有意义的那个没有编译。 如果您对此有疑问,请提供无法编译的代码。 [snip]其次,我在使用transform()时遇到了麻烦。 template< class in> bool transform(in b,const in e,in dest,In(* f)(In in)); 你确定函数f应该是从迭代器到 迭代器的映射吗? template< class in> 在mySquare中(在arg中); int main(void) C''ism。不要。 { vector< int> v1,v2; for(vector< int> :: size_type i = 1; i< = 10; ++ i) v1.push_back(i); transform(v1.begin(),v1.end(),back_inserter(v2),mySquare); v1.begin(),v1.end()和std :: back_inserter(v2)(假设后者是标准的)不是必须是相同的类型,如你所希望的那样变换的声明。 顺便说一句,当你混合自己的标准库替代品时br /> 东西,标准库的东西,确实使用明确的资格。 返回0; 不必要。如果确实提供了main的显式返回,请使用常量EXIT_SUCCESS和EXIT_FAILURE考虑 。说你在做什么 做什么。 } - 答:因为它弄乱了人们通常阅读文字的顺序。 问:为什么这么糟糕? A:热门发布。 问:usenet和电子邮件中最烦人的事情是什么? Alf P. Steinbach< al *** @ start。否GT;写道: template< class in> 在mySquare中(在arg中); int main(void) C''ism。不要。 我不明白这一点。不是吗? return 0; 不必要。如果确实提供了main的显式返回,请考虑使用常量EXIT_SUCCESS和EXIT_FAILURE。说出你在做什么。 0意味着在C中取得成功。对于C ++来说,这不再是真的吗? 我一直试图模仿Koenig和Moo的编码风格。他们不是很好的编码角色模型吗? * Peter: Alf P. Steinbach <人*** @ start.no>写道: template< class in> 在mySquare中(在arg中); int main(void) C''ism。不要。 我不明白这一点。不是吗? 不要在C ++中为空参数列表写''void''。对C中的空参数列表写''void'' 。在C中你需要''void'来指定 ''没有参数'' ,而不是任何论据;在C ++中缺少任何 正式参数意味着''没有参数'',所以在C ++中''void''表示人类读者的C / C代码 - - 而且,更多的是阅读。 return 0; 不必要。如果确实提供了main的显式返回,请考虑使用常量EXIT_SUCCESS和EXIT_FAILURE。说出你在做什么。 0意味着在C中取得成功。对于C ++来说,这是不是真的? 在C ++中,0和EXIT_SUCCESS都表示成功,通常EXIT_SUCCESS 定义为0. 这又是关于可读性和通信意图。 如果你写''返回0''那么这可能意味着''只是退出主要,我 不关心价值' ',尤其是那里没有明显的其他原因,因为那里有一个''返回''(0是默认值),而如果你这么做,那么你可以写''''返回EXIT_SUCCESS''然后毫无疑问它意味着什么: 在这种情况下它意味着返回值是有意义的,并且有或者 可能在未来是其他地方EXIT_FAILURE的可能性。 我一直试图模仿Koenig和Moo的编码风格。他们不是好的编码角色模型吗? 他们是; Andrew Koenig是一位C ++专家,也是一位优秀的专家。 Barbara Moo也可能是(我对她一无所知,而安德鲁经常 参加这个论坛)。但是没有一个人是完美的,并且有一些比完美的结构还要好一些优秀的结构,它可能是_best_这样的书。我知道有这样的不完善之处,或者你可能称之为直接缺陷,因为有些已经在这个论坛上讨论过。。简直不要指望最好的榜样是完美的。 然而,编码风格是另一回事;非常主观,我会犹豫不决说什么都是更好的在某种意义上,这比其他任何东西都要好。 如果我这样做,那只会_define_我对更好的看法,理论上可以讨论。但是有人,总会有一个 SomeOne,会把它读作绝对判断某些编码风格的价值 ,并且不同意,不仅仅应用修辞还有像洲际核导弹这样的重型武器。那个 可能意味着我必须从奥斯陆搬家...... ;-) - 答:因为它弄乱了人们通常阅读文字的顺序。 问:为什么这么糟糕? A:热门帖子。 问:usenet和电子邮件中最烦人的是什么? I''m doing exercise 8-2 from "Accelerated C++" where we''re supposed toimplement library algorithms, trying to minimize the number of iteratoroperations. I have two questions so far.First, it took me a while to figure out how to get accumulate() to compilecorrectly:// begin and begin2 must both be input iterators.//template< class In, class Out >Out accumulate( In begin, const In end, Out ){Out ret = 0;while ( begin != end )ret += *begin++;return ret;}The problem is that I don''t understand the function''s signature. When wedefine a function, each argument needs a type and an argument name. So thismakes sense to me:template< class In, class Out >Out accumulate( In begin, const In end, Out t ){...}but not this:template< class In, class Out >Out accumulate( In begin, const In end, Out ){...}Of course, the one that makes sense to me doesn''t compile. The compiler justneeds the type of the argument; the actual value is inconsequential:int i = accumulate( v.begin(), v.end(), 0 );But it still feels wierd to define a function with an argument with a type,but no name. Is there a way to better understand why the language''s syntaxis like this?Secondly, I''m having trouble with transform().template< class In >bool transform( In b, const In e, In dest, In (*f)(In in) );template< class In >In mySquare( In arg );int main( void ){vector<int> v1, v2;for ( vector<int>::size_type i = 1; i <= 10; ++i )v1.push_back(i);transform( v1.begin(), v1.end(), back_inserter(v2), mySquare );return 0;}template< class In >In mySquare( In arg ){return arg * arg;}// begin and begin2 must both be input iterators.//template< class In >bool transform( In begin, const In end, In dest, In (*f)(In in) ){while ( begin != end )*dest++ = f(*begin++);return dest;}The compiler isn''t finding any functions that match my call to transform(),so I''ve blown it somewhere. How can this code be fixed?Thanks,Peter 解决方案 * Pete: I''m doing exercise 8-2 from "Accelerated C++" where we''re supposed to implement library algorithms, trying to minimize the number of iterator operations.If that exercise is relevant to your questions you should have quotedit. I have two questions so far. First, it took me a while to figure out how to get accumulate() to compile correctly: // begin and begin2 must both be input iterators. // template< class In, class Out > Out accumulate( In begin, const In end, Out ) { Out ret = 0; while ( begin != end ) ret += *begin++; return ret; }For std::accumulate the third argument provides an initial value forwhat you call ''ret''. The problem is that I don''t understand the function''s signature. When we define a function, each argument needs a type and an argument name.No, it only needs a type. So this makes sense to me: template< class In, class Out > Out accumulate( In begin, const In end, Out t ) { ... } but not this: template< class In, class Out > Out accumulate( In begin, const In end, Out ) { ... } Of course, the one that makes sense to me doesn''t compile.If you have a question about that, do provide the code that doesn''tcompile.[snip] Secondly, I''m having trouble with transform(). template< class In > bool transform( In b, const In e, In dest, In (*f)(In in) );Are you sure the function f should be a mapping from iterator toiterator? template< class In > In mySquare( In arg ); int main( void )C''ism. Don''t. { vector<int> v1, v2; for ( vector<int>::size_type i = 1; i <= 10; ++i ) v1.push_back(i); transform( v1.begin(), v1.end(), back_inserter(v2), mySquare );v1.begin(), v1.end() and std::back_inserter(v2) (assuming the latter isthe standard one) are not necessarily of the same type, as assumed byyour declaration of transform.Btw., when you''re mixing your own replacements for standard librarythings, with standard library things, do use explicit qualification. return 0;Unnecessary. If you do provide an explicit return from main, considerusing the constants EXIT_SUCCESS and EXIT_FAILURE. Saying what you''redoing. }--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail?Alf P. Steinbach <al***@start.no> wrote: template< class In > In mySquare( In arg ); int main( void ) C''ism. Don''t.I don''t understand this. Don''t what? return 0; Unnecessary. If you do provide an explicit return from main, consider using the constants EXIT_SUCCESS and EXIT_FAILURE. Saying what you''re doing.0 means success in C. Is that no longer true for C++?I''ve been trying to emulate the coding style of Koenig and Moo. Are they notgood coding role models? * Peter: Alf P. Steinbach <al***@start.no> wrote: template< class In > In mySquare( In arg ); int main( void ) C''ism. Don''t. I don''t understand this. Don''t what?Don''t write ''void'' for an empty argument list, in C++. Do write ''void''for an empty argument list, in C. In C you need the ''void'' to specify''no arguments'', as opposed to ''any arguments''; in C++ the absence of anyformal argument means ''no arguments'', and so in C++ the ''void'' indicatesC code to the human reader -- and also, it''s more to read. return 0; Unnecessary. If you do provide an explicit return from main, consider using the constants EXIT_SUCCESS and EXIT_FAILURE. Saying what you''re doing. 0 means success in C. Is that no longer true for C++?In C++ both 0 and EXIT_SUCCESS mean success, and typically EXIT_SUCCESSis defined as 0.Again this is about readability and communicating intent.If you write ''return 0'' then that could mean ''just exit from main, Idon''t care about the value'', especially when there''s no obvious otherreason for having a ''return'' there (0 being the default), whereas if youwrite ''return EXIT_SUCCESS'' then there''s no question of what it means:in that case it means the return value is meaningful, and there is ormay in the future be some possibility of EXIT_FAILURE somewhere else. I''ve been trying to emulate the coding style of Koenig and Moo. Are they not good coding role models?They are; Andrew Koenig is a C++ expert and a good one. Barbara Moo isprobably also (I don''t know anything about her, whereas Andrew oftenparticipates in this forum). But no-one''s perfect, and there are someless than perfect constructions in that otherwise excellent, it''sprobably the _best_ such, book. I know there are such imperfections, oryou might call them direct flaws, because some have been discussed inthis forum. Simply don''t expect the best role models to be perfect.However, coding style is another matter; extremely subjective and I''dhesitate to say anything is "better" in some sense than anything else.If I did then that would just _define_ my opinion of "better", whichtheoretically could then be discussed. But someone, there''s always aSomeOne, would read that instead as an absolute judgement of the valueof some coding style, and disagree, applying not just rhetoric but alsoheavy weaponry such as intercontinental nuclear missiles. And thatcould mean I would have to move from Oslo... ;-)--A: Because it messes up the order in which people normally read text.Q: Why is it such a bad thing?A: Top-posting.Q: What is the most annoying thing on usenet and in e-mail? 这篇关于实现库算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-24 20:52