C++ STL 专家在面试中应该回答哪些问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1691759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:54:51  来源:igfitidea点击:

What questions should an expert in STL be expected to answer, in an interview

c++stl

提问by James Black

I was looking at a job posting recently and one of the requirements was that a person be a 9/10 in their knowledge of STL.

我最近在看一个职位发布,其中一个要求是一个人在 STL 的知识方面是 9/10。

When I judge my skills, to me a 10 is someone that writes advanced books on the subject, such as Jon Skeet (C#), John Resig (JavaScript) or Martin Odersky (Scala).

当我判断自己的技能时,对我来说,10 分是写了有关该主题的高级书籍的人,例如 Jon Skeet (C#)、John Resig (JavaScript) 或 Martin Odersky (Scala)。

So, a 9/10 is basically a 10, so I am not certain what would be expected at that level.

所以,9/10 基本上是 10,所以我不确定在那个级别会发生什么。

An example of some questions would be found at: http://discuss.joelonsoftware.com/default.asp?joel.3.414500.47

可以在以下位置找到一些问题的示例:http: //discuss.joelonsoftware.com/default.asp?joel.3.414500.47

Obviously some coding will be needed, but should everything be expected to be memorized, as there is quite a bit in STL.

显然需要进行一些编码,但是应该记住所有内容,因为 STL 中有相当多的内容。

In some cases Boost libraries extend STL, so should it be expected that I would be using Boost also, as I may sometimes confuse which function came from which of the two libraries.

在某些情况下,Boost 库扩展了 STL,所以我应该也使用 Boost,因为有时我可能会混淆哪个函数来自两个库中的哪个库。

I am trying to get an idea if I can answer questions that would be expected of a STL expert, though it is odd that being a C++ expert wasn't a requirement.

我想知道我是否可以回答 STL 专家所期望的问题,尽管奇怪的是,成为 C++ 专家并不是必需的。

UPDATE

更新

After reflecting on the answers to my question it appears that what they may be looking for is someone that can see the limits of STL and extend the library, which is something I haven't done. I am used to thinking within the limits of what STL and Boost give me and staying within the lines. I may need to start looking at whether that has been too limiting and see if I can go outside the box. I hope they don't mean a 9 as Google does. :)

在反思我的问题的答案后,他们可能正在寻找的是可以看到 STL 的限制并扩展库的人,这是我没有做过的事情。我习惯于在 STL 和 Boost 给我的范围内思考并保持在界限内。我可能需要开始考虑这是否太局限了,看看我是否可以跳出框框。我希望它们不像 Google 那样表示 9。:)

回答by Alex Martelli

Funny -- I don't consider myself a 9/10 in STL (I used to be, but I'm a bit rusty now), and I do fully agree with @joshperry's important terminological distinguo (I've often been on record as berating the abuse of STL to mean "the parts of the C++ standard library that were originally inspired by SGI's STL"!-), yet I consider his example code less than "optimally STL-ish". I mean, for the given task "Put all the integers in a vector to standard out.", why would anyone evercode, as @joshperry suggests,

有趣——我不认为自己是 STL 的 9/10(我曾经是,但我现在有点生疏),我完全同意@joshperry 的重要术语区分(我经常被记录在案指责滥用 STL 意味着“最初受 SGI 的 STL 启发的 C++ 标准库的部分”!-),但我认为他的示例代码不是“最佳的 STL 风格”。我的意思是,对于给定的任务“把所有的整数中的向量到标准输出。”,为什么会有人曾经代码,@joshperry表明,

for(std::vector<int>::iterator it = intVect.begin(); it != intVect.end(); ++i)
    std::cout << *it;

rather than the obvious:

而不是显而易见的:

std::copy(intVect.begin(), intVect.end(), std::ostream_iterator<int>(std::cout));

or the like?! To me, that would kind of suggest they don't knowabout std::ostream_iterator-- especially if they're supposed to be showing off their STL knowledge, why wouldn't they flaunt it?-)

之类的?!对我来说,这将那种暗示的他们不知道有关std::ostream_iterator-特别是如果他们应该展示自己的STL的知识了,他们为什么不炫耀- ?)

At my current employer, to help candidates self-rate about competence in a technology, we provide a helpful guide -- "10: I invented that technology; 9: I wrote THE book about it" and so on down. So, for example, I'd be a 9/10 in Python -- only my colleague and friend Guido can fairly claim a 10/10 there. STL is an interesting case: while Stepanov drove the design, my colleague Matt Austern did the first implementation andwrote "the" book about it, too (this one) -- so I think he'd get to claim, if not a 10, a 9.5. By that standard, I could be somewhere between 7 and 8 if I could take an hour to refresh (custom allocators and traits are always tricky, or at least that's how I recall them!-).

在我现在的雇主,为了帮助求职者对某项技术的能力进行自我评价,我们提供了一个有用的指南——“10:我发明了这项技术;9:我写了关于它的书”等等。因此,例如,我在 Python 中的得分为 9/10 —— 只有我的同事和朋友 Guido 可以在那里公平地声称为 10/10。STL 是一个有趣的案例:当 Stepanov 推动设计时,我的同事 Matt Austern 进行了第一个实现,并且也写了关于它的“the”书(这本)——所以我认为他会声称,如果不是 10 ,一个 9.5。按照这个标准,如果我可以花一个小时来刷新,我可能会在 7 到 8 之间(自定义分配器和特征总是很棘手,或者至少我是这样回忆它们的!-)。

So, if you're probing somebody who claims a 9, grill them over the really hard parts such as custom allocators and traits -- presumably they wouldn't miss a beat on all the containers, algorithms, and special iterators, so don't waste much interview time on those (which would be key if you were probing for a 7 or 7.5). Maybe ask them to give a real-life example where they used custom traits and/or allocators, and code all the details of the implementation as well as a few sample uses.

所以,如果你正在调查那些声称 9 的人,请在真正困难的部分(例如自定义分配器和特征)上对他们进行烧烤——大概他们不会错过所有容器、算法和特殊迭代器的节拍,所以不要“不要在这些上浪费太多面试时间(如果你想考 7 分或 7.5 分,这将是关键)。也许让他们举一个真实的例子,他们使用自定义特征和/或分配器,并编写实现的所有细节以及一些示例用法。

BTW, if you're the one needing to cramon C++'s standard library at an advanced level, I'm told by knowledgeable and non-rusty friends that Josuttis' booknowadays is even more useful than my friend Matt's (unfortunately, I've never read Josuttis in depth, so I can't confirm or deny that - I do see the book has five stars on Amazon, which isimpressive;-).

顺便说一句,如果你是一个需要到补习班上的C ++标准库在一个先进的水平,我的知识和非生锈的朋友告诉记者,约祖蒂斯“的书现在甚至比我的朋友马特(不幸的是,我有更多的有用从来没有深入阅读约祖蒂斯,所以我无法证实或否认-我看的书有亚马逊,这五颗星令人印象深刻的;-)。

回答by tony

It is just a dumb requirement for a job. When hiring you want a goodgreat programmer first, specific knowledge second.

这只是对工作的愚蠢要求。在招聘时,您首先需要一个优秀的程序员,其次需要特定的知识。

It would be reasonable, at this day and age, to expect knowledge/familiarity/etc with the STL. But unless the job is to reimplement the STL, you don't need 9/10. Even if that isthe job, you still just need a great programmer that has lots of experience with templates (making not just using).

在这个时代,期望对 STL 有知识/熟悉/等是合理的。但除非工作是重新实现 STL,否则您不需要 9/10。即使那工作,您仍然只需要一个拥有丰富模板经验的优秀程序员(不仅仅是使用)。

For example, for all the answers to "output the integers of a vector", probably the exact same code is generated. Only the version that has been templated to handle any container of any items shows a hint of 'great' vs good (just a hint). ie the ability to abstract.

例如,对于“输出向量的整数”的所有答案,可能会生成完全相同的代码。只有经过模板化以处理任何项目的任何容器的版本才显示出“好”与“好”的暗示(只是一个提示)。即抽象能力。

Anyhow, just go for it. Be ready to use the STL to help solve other problems. Nothing more.

不管怎样,就去做吧。准备好使用 STL 来帮助解决其他问题。而已。

(In fact, for most of the interviews I've been in, the requirement was to NOT use the STL. ie - write a function that reverses a string. My first answer is that there is probably something in the std lib that would do that. Then they say, right, of course, but what if you had to write it yourself...)

(事实上​​,对于我参加过的大多数面试,要求是不要使用 STL。即 - 编写一个反转字符串的函数。我的第一个答案是 std lib 中可能有一些东西可以做那个。然后他们说,对,当然,但如果你必须自己写呢......)

回答by Jerry Coffin

I should preface this by noting that I think the same criteria should be applied not only to STL (regardless of which definition you prefer for that), but to many other types of things as well.

我应该首先指出,我认为相同的标准不仅应该应用于 STL(无论您喜欢哪种定义),而且还应用于许多其他类型的事物。

From my perspective, simply knowing the existing STL components and being able to apply them well probably should not qualify as a 9/10. Rather, I'd consider that level about 7/10. 8/10 is when the person is able to extendthe STL by providing new components that follow its philosophy and fit with existing components naturally and easily.

从我的角度来看,仅仅了解现有的 STL 组件并能够很好地应用它们可能不符合 9/10 的标准。相反,我认为该级别约为 7/10。8/10 是人们能够通过提供遵循其理念并自然轻松地与现有组件匹配的新组件来扩展STL。

By 9/10, I'd expect to see somebody who can not only provide newcomponents, but is able to improve some of the existing ones, such as Boost::bind. For 10/10, I'd expect to see this go beyond the rather ad hoc, localized improvements of a 9/10, and moving toward looking at a more architectural level, such as using ranges instead of individual iterators. For a concrete example, consider the difference between Boost's ranges and Andrei Alexandrescu's ideas for ranges. Boost's ranges are handy, useful and convenient, but they change what you type, not how you think. Andre's version of ranges is much more all-encompassing -- an architectural solution that changes how you design and think about code, not just how you type it.

到 9/10,我希望看到有人不仅可以提供组件,还可以改进一些现有组件,例如 Boost::bind。对于 10/10,我希望看到这超出了 9/10 的相当临时的、本地化的改进,并转向着眼于更多的架构级别,例如使用范围而不是单个迭代器。举一个具体的例子,考虑一下 Boost 的范围和 Andrei Alexandrescu 的范围想法之间的区别。Boost 的范围方便、有用且方便,但它们会改变您键入的内容,而不是您的想法。安德烈的范围版本更加无所不包——一种架构解决方案,它改变了您设计和思考代码的方式,而不仅仅是您键入的方式。

回答by navigator

9/10 is quite subjective. I have been asked good questions about STL. These are examples:

9/10 是相当主观的。有人问我关于 STL 的好问题。这些是示例:

  • When should you use a deque vs a vector (knowledge of how they are internally implemented is helpful)
  • Recognize STL code that uses invalid references, or can end up using invalid references.
  • Implement simple operations on different containers, and know where and when to use std::algorithm vs member functions of a container.
  • 什么时候应该使用双端队列和向量(了解它们如何在内部实现是有帮助的)
  • 识别使用无效引用或可能最终使用无效引用的 STL 代码。
  • 在不同的容器上实现简单的操作,并且知道在何处和何时使用 std::algorithm 与容器的成员函数。

回答by joshperry

Well, you could walk into the interview and say "I noticed that your posting asked for someone knowledgeable in STL but that term is sometimes used to mean: (1) C++ standard library; (2) the library Stepanov designed at HP; (3) the parts of [1] based on [2]; (4) specific vendor implementations of either [1], [2], or [3]; (5) the underlying principles of [2]. As such, the term is highly ambiguous, and must be used with extreme caution. If you meant [1] and insist on abbreviating, "stdlib" is a far better choice."*

好吧,你可以走进面试并说“我注意到你的帖子要求了解 STL 的人,但该术语有时用于表示:(1) C++ 标准库;(2) Stepanov 在 HP 设计的库;(3 ) 基于 [2] 的 [1] 部分;(4) [1]、[2] 或 [3] 的特定供应商实现;(5) [2] 的基本原则。因此,术语高度含糊不清,必须极其谨慎地使用。如果您的意思是 [1] 并坚持缩写,那么“stdlib”是一个更好的选择。"*

Honestly though since it is a library it is somewhat finite and probably not composeable to nauseating infinitum like a language proper. So I would say any question that had them use some of the stdlib algorithms would be effective to see if they knew them well.

老实说,因为它是一个库,所以它有点有限,并且可能不像语言本身那样令人作呕。所以我会说任何让他们使用某些 stdlib 算法的问题都可以有效地查看他们是否熟悉它们。

With iterators being an integral part of the stdlib I would also maybe ask them to "Put all the integers in a vector to standard out." I would expect something like:

由于迭代器是 stdlib 的一个组成部分,我也可能会要求他们“将向量中的所有整数放入标准输出”。我希望是这样的:

// thanks to onebyone
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, " ");

If they write something like the following they probably aren't very familiar with iterators:

如果他们编写如下内容,他们可能对迭代器不太熟悉:

for(int i = 0; i < vec.size(); ++i)
    std::cout << vec[i];

Also one interesting thing to look for is if they do using namespace stdat the top of their code file. Ask them why, and if they don't say something along the lines of "I use that for short demo code only" or if they put it in a header file, thank them for coming in and send them out the door.

还有一件有趣的事情是,他们是否using namespace std在代码文件的顶部这样做。问他们为什么,如果他们没有说“我只将它用于简短的演示代码”,或者如果他们把它放在头文件中,感谢他们进来并把他们送出去。

Another aspect of the stdlib is it's heavy use of templates, the person should have a good understanding of basic template programing for type substitution. Maybe ask them to "Write a function that will write all of items of any stdlib container to standard out". I would expect to see something like:

stdlib 的另一个方面是它大量使用模板,该人应该对用于类型替换的基本模板编程有很好的理解。也许让他们“编写一个函数,将任何 stdlib 容器的所有项目写入标准输出”。我希望看到类似的东西:

template<typename InputIter>
void Output(InputIter it, InputIter end) {
    while(it != end)
        std::cout << *it++;
}

These are probably not 9/10 questions but interesting ones I think a 2-3/10 should know.

这些可能不是 9/10 的问题,而是有趣的问题,我认为 2-3/10 应该知道。

One 9/10 difficulty I would say is to write a derived iostream properly without using the boost stream base classes. But there is probably quite a difference between usingthe stdlib and extendingit...

我要说的一个 9/10 困难是在不使用 boost 流基类的情况下正确编写派生的 iostream。但是使用stdlib 和扩展它之间可能有很大的不同......

*(thanks to nolyc on freenode ##C++ for the quote)

*(感谢 freenode ##C++ 上的 nolyc 引用)