C++ 作为第一语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/713704/
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 16:54:49  来源:igfitidea点击:

C++ as a first language

c++

提问by Skilldrick

I've been self-learning C++ for about 4 months now. I'm enjoying it, and I think I'm doing quite well. However, an answer to a question of minegot me thinking that I might be setting myself up for a fall.

我已经自学 C++ 大约 4 个月了。我很享受,我认为我做得很好。然而,对我的一个问题的回答让我想到我可能正在为跌倒做准备。

So, what do people here think about C++ as a first language to learn? And is it worth me just carrying on now that I've made a good start?

那么,这里的人们如何看待 C++ 作为第一门学习语言?现在我已经有了一个好的开始,值得我继续吗?

回答by DevSolar

I don't understand why people still confuse "language" with "library". (Referring to the linked answer.) So what if C++ doesn't have a "native" concept of audio? There are lots of libraries out there, which you can readily use with C++, and which are probably better suited to your specific needs than any "catch-all" "standard-library" audio processing API could be.

我不明白为什么人们仍然将“语言”与“图书馆”混淆。(请参阅链接的答案。)那么,如果 C++ 没有“本机”音频概念呢?有很多库,您可以很容易地与 C++ 一起使用,并且它们可能比任何“全能”“标准库”音频处理 API 更适合您的特定需求。

</rant>

</rant>

C++ is a difficult language. There are others that are easier to learn. I would never argue about that.

C++是一门难学的语言。还有其他更容易学习的。我永远不会为此争论。

But C++ is easily one of the most powerfullanguages around. It can be highly efficient, and highly elegant, at once. Of course, nothing keeps you from making a fine big mess of everything, either.

但是 C++ 很容易成为最强大的语言之一。它可以是高效的,具有很强的优雅,在一次。当然,也没有什么能阻止你把一切都搞得一团糟。

If I recommend C++ as a first programming language? Actually, I don't know any better. Others might protect you from making mistakes, and make initial success (e.g. your first GUI application) easier. But if you build on a foundation of C++, you will always be on a secure footing. You might never have to chose another language at all, actually. But if you want / have to, you will find it ridiculously easy.

如果我推荐 C++ 作为第一编程语言?其实,我不知道更好。其他人可能会保护您免于犯错,并使最初的成功(例如您的第一个 GUI 应用程序)更容易。但是,如果您建立在 C++ 的基础上,您将始终处于安全的基础之上。实际上,您可能根本不必选择另一种语言。但是,如果您愿意/必须这样做,您会发现这非常容易。

An experienced C++ coder can do e.g. Java at full speed in a matter of weeks. The other way round? Much, muchmore difficult.

一个有经验的 C++ 编码员可以在几周内全速完成 Java 等工作。反过来呢?很多,很多更加困难。



Many years later, I felt like I should amend this answer. Since my kids asked me to teach them programming, I started to do so. I found myself actually notstarting with C++... because I showed them BFfirst. In absolutely no time at all, they understood about memory and pointers. Then I went on with C++, and we're enjoying ourselves immensely so far.

许多年后,我觉得我应该修改这个答案。自从我的孩子让我教他们编程以来,我就开始这样做了。我发现自己实际上并不是从 C++ 开始的……因为我首先向他们展示了BF。他们很快就理解了内存和指针。然后我继续使用 C++,到目前为止我们非常享受。



Yet some more years later, and I felt I really should add this excellent lecture by Kate Gregory from CppCon 2015 to this answer:

然而又过了几年,我觉得我真的应该把凯特·格雷戈里 (Kate Gregory) 在 2015 年 CppCon 上的精彩演讲添加到这个答案中:

Stop Teaching C.

停止教授C

回答by tpdi

Let me put it this way:

让我这样说吧:

I took and passed with a 94%, the Sun Java Certification without ever having compiled a line of Java(that was nine years ago). I was able to do this, because I knew C++ very well.

我以 94% 的成绩通过了 Sun Java 认证,而我从未编译过任何 Java 代码(那是九年前的事)。我能够做到这一点,因为我非常了解 C++。

On my first professional job, I was hired as a tester. On my second day, I was told I could program instead of test, if I learned enough PowerBuilder to be useful -- in two weeks. Because I knew C++ so well, PowerBuilder was easy.

在我的第一份专业工作中,我被聘为测试员。第二天,我被告知我可以编程而不是测试,如果我在两周内学会了足够有用的 PowerBuilder。因为我非常了解 C++,所以 PowerBuilder 很容易。

C++ is hard, but if you learn it well -- read Stroustrup's The C++ Programming Language, do the exercises too, read his Design and Evolution of C++, read the C++ Faq and the Meyers books and Herb Sutter's books, read comp.lang.c++.moderated, read Coplien's Advanced C++ Programming Styles and Idiomsno matter what OO language you want to code in -- if you learn it well, every other OO language is easy.

C++很难,但如果你学得好——阅读 Stroustrup 的The C++ Programming Language,也做练习,阅读他的 C++ 的设计和进化,阅读 C++ Faq 和 Meyers 的书籍以及 Herb Sutter 的书籍,阅读 comp.lang。 c++.moderated,阅读 Coplien 的Advanced C++ Programming Styles and Idioms,不管你想用什么 OO 语言编写代码——如果你学得好,其他任何 OO 语言都很容易。

Learning C++ is excellent preparation for learning to code. (And you'll become a fair C programmer too, but read K&R to get to be a good C programmer.)

学习 C++ 是学习编码的绝佳准备。(你也会成为一名优秀的 C 程序员,但阅读 K&R 才能成为一名优秀的 C 程序员。)

Oh. And C++ is really fun to program in, muchless verbose than Java, and much more flexible. You're really in charge, not the IDE or the language. You can shoot off your own foot, but you'll be in charge of doing it. And read Alexandrescu's book once you know templates, it'll amaze and dumbfound and delight you.

哦。和C ++真的很好玩在,节目更简洁比Java,并且更加灵活。您真正负责,而不是 IDE 或语言。你可以用自己的脚射门,但你要负责。一旦你了解了模板,阅读 Alexandrescu 的书,它会让你惊讶、目瞪口呆和高兴。

C++ is fun. Read the "Curiously Recurring Template Pattern": it looks like this

C++ 很有趣。阅读“Curiously Recurring Template Pattern”:它看起来像这样

template< class  T> struct base {};

struct derived: base< derived > {};

Yeah, you got that right: it's a class that derives from a base that's templated on the deriving class. Now that sounds just obscure and silly, but there are many good uses for it, and just the other day I was beating my head because you can't do this in Java(because Java uses type erase, not templates, for generics), but if I could have used it, I could have saved lots and lots of code repeated in lots of classes.

是的,你说得对:它是一个派生自以派生类为模板的基类的类。现在这听起来很晦涩和愚蠢,但它有很多很好的用途,就在前几天,我感到震惊,因为你不能在 Java 中做到这一点(因为 Java 使用类型擦除,而不是模板,用于泛型),但是如果我可以使用它,我就可以在很多类中保存大量重复的代码。

There's so much more in C++, and if you limit yourself to Java or .Net, you'll never discover it.

C++ 中有很多东西,如果你只使用 Java 或 .Net,你永远不会发现它。

回答by D.Shawley

C++ isn't an easy language to learn as noted by everyone else, but there are a few reasons that it makes a decent place to start:

正如其他人所指出的那样,C++ 并不是一门容易学习的语言,但有几个原因使它成为一个不错的起点:

  • it doesn't tie you to a specific programming paradigm (e.g., Java and OOP) so you can experiment with different techniques easily
  • C++ doesn't have the bells and whistles so you can concentrate on making the building blocks if you want to know how they are built
  • since it is a language and not a platform, you can choose the library or libraries that best fit the task at hand or your chosen programming style (e.g., choosing something like Pocofor a Javaesque feel, ACEfor the gritty early style OO, etc.)
  • it is a general purpose programming language, not a platform (had to say that twice)
  • 它不会将您绑定到特定的编程范式(例如 Java 和 OOP),因此您可以轻松地尝试不同的技术
  • C++ 没有花里胡哨的东西,所以如果你想知道它们是如何构建的,你可以专注于构建构建块
  • 由于它是一种语言而不是平台,因此您可以选择最适合手头任务或您选择的编程风格的库或库(例如,选择Poco 之类的东西以获得 Javaesque 的感觉,选择ACE代表坚韧不拔的早期风格 OO 等.)
  • 它是一种通用编程语言,而不是一个平台(不得不说两次)

The trick is learning it carefully and correctly the first time. I know that I, like many others, have struggled with it by learning it as "C + objects" before really understanding it. It is a hard language to learn. I would recommend looking for an experienced C++ mentor to attach yourself to as well as reading a lot of the "highly recommended" books on how to and how not to write C++. Avoid learning bad habits up front.

诀窍是第一次仔细正确地学习它。我知道我和许多其他人一样,在真正理解它之前通过将它作为“C + 对象”学习它而挣扎。这是一门难学的语言。我建议您寻找一位经验丰富的 C++ 导师,并阅读大量关于如何编写 C++ 以及如何不编写 C++ 的“强烈推荐”书籍。避免预先学习坏习惯。

For a starting reading list in no particular order:

对于没有特定顺序的起始阅读列表:

  • "Accelerated C++" by Koenig and Moo
  • "Effective C++/STL" by Scott Meyers
  • "Object-Oriented Software Construction" by Bertrand Meyer
  • "Expert C Programming" by Peter van der Linden
  • "STL Tutorial and Reference Guide" by Musser, Derge, and Saini
  • "The C++ Standard Library" by Josuttis
  • Koenig 和 Moo 的“加速 C++”
  • Scott Meyers 的“Effective C++/STL”
  • Bertrand Meyer 的“面向对象的软件构建”
  • Peter van der Linden 的“专家 C 编程”
  • Musser、Derge 和 Saini 的“STL 教程和参考指南”
  • Josuttis 的“C++ 标准库”

回答by simon

Lots of support for c++ here, but I have to disagree. Every language has it's plusses and minuses (some achieve a better balance than others, of course).

这里有很多对 c++ 的支持,但我不同意。每种语言都有其优点和缺点(当然,有些语言比其他语言取得了更好的平衡)。

That being said, for a first language, I'm convinced most people will learn how to programmore effectively with a language that a) doesn't have an edit-compile-run cycle (which is inherently slower for you as the programmer b) has garbage collection c) has good runtime introspection. Best of if there is an interactive evaluator as well, to mess about with.

话虽如此,对于第一种语言,我相信大多数人会学习如何使用一种语言更有效地进行编程: ) 具有垃圾收集 c) 具有良好的运行时内省。最好还有一个交互式评估器,可以乱搞。

Development (if not runtime) is almost invariably faster in such languages, there are fewer things to trip you up, and there are better tools to help figure what went wrong when it did.

在这些语言中,开发(如果不是运行时)几乎总是更快,让你绊倒的东西更少,并且有更好的工具来帮助找出出错的地方。

Which isn't to say learning the vagaries of memory management in a language like c++ isn't something worth doing, etc., etc., it just isn't the place you really want to start.

这并不是说在像 c++ 这样的语言中学习内存管理的变幻莫测是不值得做的事情等等,它只是不是你真正想要开始的地方。

Another issue with c++ is that for all its practicality the sheer size of it added to the fact it has incompatible parts that can lead to subtle issues best avoided as a beginner.

C++ 的另一个问题是,尽管它具有实用性,但它的庞大规模加上它具有不兼容的部分,这可能导致初学者最好避免出现微妙的问题。

edited: John D brings up the point in a comment that some people who learn first on a GC'd language (which is by no means limited to "scripting" languages) never learn pointers later. This is a red herring, though. Some people fail to learn all sorts of things, for all sorts of reasons. I maintain that worrying about pointers is a distraction when you're learning the basics. At some point you may well need to learn about manual memory management, but it's not fundamentally important to do this at first. Or more accurately, the gains made by avoiding this are more important than the costs.

编辑:John D 在评论中提出了这一点,即一些首先学习 GC 语言(绝不限于“脚本”语言)的人以后永远不会学习指针。不过,这是一条红鲱鱼。有些人因为各种原因学不到各种东西。我认为在学习基础知识时担心指针会分散注意力。在某些时候,您可能需要了解手动内存管理,但一开始这样做并不重要。或者更准确地说,避免这种情况所带来的收益比成本更重要。

Let me be clear though, I'm not saying that c++ is an inferior choice for a first language because it lacks garbage collection. That's just one of many issues that get in the way of learning the fundamentals of programming well. The real problem is that it is a large complicated language, with quirks all over the place and even internal inconsistencies (e.g. pointers and exceptions don't play well together). Of course all of this can be managed properly and you can learn good practice to keep from getting in trouble.

不过让我说清楚,我并不是说 c++ 是第一语言的次等选择,因为它缺乏垃圾收集。这只是妨碍学习编程基础知识的众多问题之一。真正的问题是它是一种大型复杂语言,到处都有怪癖甚至内部不一致(例如,指针和异常不能很好地协同工作)。当然,所有这些都可以得到妥善管理,您可以学习良好的做法,以免陷入困境。

However, none of this will make much sense to a real neophyte at first. So some of learning it will end up being "do it this way, not that way". "why?" "because I said so --- you won't understand the details yet". Hardly an auspicious start.

然而,对于一个真正的新手来说,这一切一开始都没有多大意义。所以一些学习它最终将是“这样做,而不是那样”。“为什么?” “因为我是这么说的——你还不会明白细节”。很难有一个吉祥的开始。

The bigges gains for learning a first language though are probably in avoiding a edit-compile-run cycle (of any kind) and having good introspection.

不过,学习第一语言的最大好处可能是避免了编辑-编译-运行循环(任何类型的)并具有良好的内省。

回答by Dаn

C++ will be (much?) more approachable if you learn "modern C++" rather than "C plus some new stuff". For example, use std::string, std::vector<>, and unique_ptr<>/ shared_ptr<>instead C-style strings, arrays, and pointers.

如果你学习“现代 C++”而不是“C 加上一些新东西”,C++ 会(很多?)更平易近人。例如,使用std::stringstd::vector<>unique_ptr<>/shared_ptr<>代替 C 风格的字符串、数组和指针。

The already mentioned Accelerated C++by Koenig and Moois one book that does exactly this.

已经提到的加速C ++科尼希和武是一个书,正是这一点。

回答by z -

any language is a good first language (within reason, not something like lolcode)

任何语言都是好的第一语言(在合理范围内,而不是像 lolcode 这样的东西)

once you get the hang of how it works, it becomes easier to branch out to other languages.

一旦掌握了它的工作原理,就可以更轻松地扩展到其他语言。

回答by ólafur Waage

C++ as a first language can be OK if you have a good teacher. But it's a pretty high wall to scale for the first time, but once you scale that wall, other similar languages come very easy.

如果你有一个好老师,C++ 作为第一语言也可以。但是这是第一次可以扩展的相当高的墙,但是一旦你扩展了那堵墙,其他类似的语言就变得很容易了。

So yes, carry on.

所以是的,继续。

回答by TheTXI

I think that C++ might not be the most user-friendly language to develop in for a first timer, but it is also not going to hold your hand and necessarily lead you towards too many bad habits (I didn't say any, just not too many).

我认为 C++ 可能不是最适合初学者的开发语言,但它也不会牵着你的手,必然会导致你养成太多坏习惯(我没有说,只是没有太多)。

C++ was the introductory language that my university's programming courses were based in and that's where we learned all the basics and about data structures and algorithms before branching out into the languages of our choice.

C++ 是我所在大学的编程课程所基于的介绍性语言,在扩展到我们选择的语言之前,我们在那里学习了所有基础知识以及有关数据结构和算法的知识。

回答by Binary Worrier

For me learning C++ is mostly about learning how NOTto shoot yourself in the foot. It can be a painful exercise as with C++ there is always another edge case that will make the gun go off as it's pointing at your hoof.

对我来说,学习 C++ 主要是学习如何向自己的脚开枪。这可能是一个痛苦的练习,因为使用 C++ 总是有另一个边缘情况会使枪在指向你的蹄子时熄灭。

The up shot is, if you manage to finish the exercise, you'll be a better developer for it, and you will be able to spot future foot shooting opportunities before they happen, in what ever languages you end up using.

最好的结果是,如果您设法完成练习,您将成为更好的开发人员,并且您将能够在未来发生之前以您最终使用的语言发现足部投篮机会。

Also, when some third part library shoots it's self in the foot, you'll be better positioned to grok what went wrong than others that haven't repeatedly shot their feet for months on end.

此外,当某些第三方库自行射击时,与其他几个月没有反复射击的人相比,您可以更好地了解出了什么问题。

I'd say stick with C++, it's not a waste of time, even if you never write a single line of production C++ code.

我会说坚持使用 C++,这不是浪费时间,即使您从未编写过一行生产 C++ 代码。

Hope this helps :)

希望这可以帮助 :)

BW

体重

回答by ggambett

Carry on. C++ is still widely used and THE language for certain kind of work. So it depends on what you want to do - systems programming is done in C++ or C, business and web apps tend to be done in other things.

继续。C++ 仍然被广泛使用,并且语言用于某些类型的工作。所以这取决于你想做什么——系统编程是用 C++ 或 C 来完成的,商业和网络应用程序往往是用其他东西来完成的。

I do strongly recommend that you explore other languages as well. Python is my second language, for example; it may give you new insights about object-oriented and functional programming and make you a better C++ programmer (and a better programmer overall).

我强烈建议您也探索其他语言。例如,Python 是我的第二语言;它可能会给你关于面向对象和函数式编程的新见解,并使你成为一个更好的 C++ 程序员(以及一个更好的程序员)。

I believe being exposed to as many languages (and, more importantly, to many different programming paradigms) will make you a better programmer overall. Take a look at Haskell, Prolog, Lisp, and an Assembler variant, at least.

我相信接触尽可能多的语言(更重要的是,接触许多不同的编程范式)会让你成为一个更好的程序员。至少看看 Haskell、Prolog、Lisp 和汇编程序变体。