好主意/坏主意我应该重新实现大多数C ++? [英] Good Idea / Bad Idea Should I Reimplement Most Of C++?

查看:85
本文介绍了好主意/坏主意我应该重新实现大多数C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,在阅读这篇文章后,我发现了一个危险的想法。这个想法可以这样表示:



我不需要大部分的C ++标准库提供。那么,为什么我不实现一个较不一般的,但更容易使用的版本?



例如,使用STL吐出了不可理解的和受损的编译器错误。但是,我不在乎分配器,迭代器等。所以为什么不花几个小时,并实现一个易于使用的链表类,例如?



我想从StackOverflow社区知道这是:C ++中大多数现有功能的滚动我自己的危险,可能的缺点和可能的优势是什么?



编辑:强>我觉得人们误解了我这个想法。这个想法是要了解我是否可以实现一个非常的一小组STL功能,大大简化 - 更像一个项目教我如何数据结构等。我不建议从头开始重新整理整个轮子,只是我需要并想了解的部分。 我想我想知道的是,使用STL的复杂性是否允许创建更小,更简单的版本本身。



strong>重新使用boost或类似



我为大学编写的大部分内容,我们不允许使用外部库。因此,它是C ++标准库或我自己的类。



此问题的客观性



这个问题是主观的。也不应该是社区Wiki,因为它不是民意调查。我想要具体的论点,强调可能可能在我的方法发生的一个优势或一个劣势。与流行的信念相反,这不是意见,而是基于经验或良好的逻辑论据。



格式



请只发布一个缺点每个答案一个优势。



请...



没有宗教战争。我不是任何语言的粉丝男孩。我使用任何适用的。对于图形和数据压缩(我目前工作的),似乎是C ++。

解决方案


我实现一个较少的
一般,但更容易使用版本?


因为你不能。因为无论你对C ++有什么意见,它不是一个简单的语言,如果你还不是很好,你的链表的实现将是bug。



老实说,你的选择很简单:



学习C ++,或不使用它。是的,C ++通常用于图形,但Java也有OpenGL库。 C#,Python和几乎所有其他语言。或C.你不必使用C ++。



但如果你使用它,学习它并正确使用它。



如果你想要不可变的字符串,创建你的字符串作为const。



无论它的底层实现,STL使用非常简单。 p>

可以读取 C ++编译器错误,但需要一些练习。但更重要的是,它们不是STL代码的唯一。你会遇到他们,无论你做什么,以及你使用的图书馆。所以习惯了他们。



除此之外,还有一些其他的缺点:




  • 没有其他人会理解您的代码。如果你问一个关于std :: vector或双向迭代器的问题,每个人都相当熟悉c ++可以回答。如果你问我的:: CustomLinkedList,没有人可以帮助你。这是不幸的,因为滚动你自己也意味着会有更多的错误来寻求帮助。

  • 你试图治愈症状,而不是原因。问题是你不明白C ++。 STL只是一个症状。避免STL不会神奇地使您的C ++代码更好地工作。

  • 编译器错误。是的,他们很讨厌阅读,但他们在那里。在STL中的大量工作已经确保错误使用将在大多数情况下触发编译器错误。在C ++中,编写代码很容易,但不工作。或者似乎工作。或者在我的电脑上工作,但在其他地方失败神秘。您自己的链接列表几乎肯定会将更多错误移动到运行时,在那里他们会被检测到一段时间,并且更难跟踪。

  • 再次,它将是bug 。相信我。我看到了很好的C ++程序员在C ++中编写一个链表,只是为了发现bug之后的bug,在晦涩的边框情况下。和C ++是所有边界的情况。你的链表会正确处理异常安全吗?如果创建一个新的节点(从而调用对象类型的构造函数)抛出一个异常,它会保证一切都在一致的状态吗?它不会泄漏内存,所有适当的析构函数将被调用?它会是类型安全吗?它会是表演者吗?在C ++中编写容器类时,有很多令人头痛的问题。

  • 您错过了现有最强大和灵活的库之一,在 any 语言。 STL可以做很多,这将是一个痛苦,即使使用Java的巨大blo肿的类库。 C ++已经足够了,不必抛弃它提供的几个优点。




关注分配器,
迭代器等


分配器可以安全地忽略。你几乎不需要知道他们存在。迭代器是辉煌的,虽然,计算出来会救你很多头痛。只有三个概念你需要理解,以有效地使用STL:




  • 容器:你已经知道这些。

  • 迭代器:允许您在内存中导航容器(或容器的子集,或任何其他值序列)的抽象

  • 算法:在任何迭代器对上工作的常用算法。



是的,与Java的库相比,STL很小,但它包装当你结合上述3个概念时,一个惊人的力量。有一点学习曲线,因为它是一个不寻常的图书馆。但是如果你要花费一两天以上的C ++,这是值得学习的。



不,我不遵循你的答案格式,因为我以为实际上给你一个详细的答案会更有帮助。 ;)



编辑:



滚动自己的优势是,你会学到更多的语言,甚至为什么STL是其中一个节省的恩典。但我不是真的相信它是真的。



正如我上面所说的,很容易写出似乎似乎工作的C ++代码。当它停止工作时,很容易重新排列一些东西,例如变量的声明顺序,或在类中插入一些填充,以使它看起来像是再次工作。你会从中学到什么?这会教你如何写更好的C ++?也许。但很可能,它只是教你C ++吸吮。它会教你如何使用STL?当然不。
一个更有用的方法可能是利用StackOverflow的真棒力量在学习STL的正确方法。 :)


Recently, I've got a dangerous idea into my head after reading this blog post. That idea can be expressed like this:

I don't need most of what the C++ standard library offers. So, why don't I implement a less general, but easier to use version?

As an example, using the STL spits out reams of incomprehensible and mangled compiler errors. But, I don't care about allocators, iterators and the like. So why don't I take a couple of hours and implement an easy to use linked list class, for example?

What I'd like to know from the StackOverflow community is this: what are the dangers, possible disadvantages and possible advantages to "rolling my own" for most of the existing functionality in C++?

Edit: I feel that people have misunderstood me about this idea. The idea was to understand whether I could implement a very small set of STL functionality that is greatly simplified - more as a project to teach me about data structures and the like. I don't propose re-inventing the entire wheel from the ground up, just the part that I need and want to learn about. I suppose what I wanted to figure out is whether the complexity of using the STL warrants the creation of smaller, simpler version of itself.

Re-using boost or similiar.

Most of what I code is for University and we're not allowed to use external libraries. So it's either the C++ standard library, or my own classes.

Objectivity of this question.

This question is not subjective. Nor should it be community Wiki, since it's not a poll. I want concrete arguments that highlight one advantage or one disadvantage that could possibly occur with my approach. Contrary to popular belief, this is not opinion, but based on experience or good logical arguments.

Format.

Please post only one disadvantage or one advantage per answer. This will allow people to evaluate individual ideas instead of all your ideas at once.

And please...

No religious wars. I'm not a fan boy of any language. I use whatever's applicable. For graphics and data compression (what I'm working on at the moment) that seems to be C++. Please constrain your answers to the question or they will be downvoted.

解决方案

So, why don't I implement a less general, but easier to use version?

Because you can't. Because whatever else you might say about C++, it is not a simple language, and if you're not already very good at it, your linked list implementation will be buggy.

Honestly, your choice is simple:

Learn C++, or don't use it. Yes, C++ is commonly used for graphics, but Java has OpenGL libraries too. So does C#, Python and virtually every other language. Or C. You don't have to use C++.

But if you do use it, learn it and use it properly.

If you want immutable strings, create your string as const.

And regardless of its underlying implementation, the STL is remarkably simple to use.

C++ compiler errors can be read, but it takes a bit of practice. But more importantly, they are not exclusive to STL code. You'll encounter them no matter what you do, and which libraries you use. So get used to them. And if you're getting used to them anyway, you might as well use STL too.

Apart from that, a few other disadvantages:

  • No one else will understand your code. If you ask a question on SO about std::vector, or bidirectional iterators, everyone who's reasonably familiar with c++ can answer. If you ask abut My::CustomLinkedList, no one can help you. Which is unfortunate, because rolling your own also means that there will be more bugs to ask for help about.
  • You're trying to cure the symptom, rather than the cause. The problem is that you don't understand C++. STL is just a symptom of that. Avoiding STL won't magically make your C++ code work better.
  • The compiler errors. Yes, they're nasty to read, but they're there. A lot of work in the STL has gone into ensuring that wrong use will trigger compiler errors in most cases. In C++ it's very easy to make code that compiles, but doesn't work. Or seems to work. Or works on my computer, but fails mysteriously elsewhere. Your own linked list would almost certainly move more errors to runtime, where they'd go undetected for a while, and be much harder to track down.
  • And once again, it will be buggy. Trust me. I've seen damn good C++ programmers write a linked list in C++ only to uncover bug after bug, in obscure border cases. And C++ is all border cases. Will your linked list handle exception safety correctly? Will it guarantee that everything is in a consistent state if creating a new node (and thereby calling the object type's constructor) throws an exception? That it won't leak memory, that all the appropriate destructors will be called? Will it be as type-safe? Will it be as performant? There are a lot of headaches to deal with when writing container classes in C++.
  • You're missing out on one of the most powerful and flexible libraries in existence, in any language. The STL can do a lot that would be a pain even with Java's giant bloated class library. C++ is hard enough already, no need to throw away the few advantages it offers.

I don't care about allocators, iterators and the like

Allocators can be safely ignored. You pretty much don't even need to know that they exist. Iterators are brilliant though, and figuring them out would save you a lot of headaches. There are only three concepts you need to understand to use STL effectively:

  • Containers: You already know about these. vectors, linked lists, maps, sets, queues and so on.
  • Iterators: Abstractions that let you navigate a container (or subsets of a container, or any other sequence of value, in memory, on disk in the form of streams, or computed on the fly).
  • Algorithms: Common algorithms that work on any pair of iterators. You have sort, for_each, find, copy and many others.

Yes, the STL is small compared to Java's library, but it packs a surprising amount of power when you combine the above 3 concepts. There's a bit of a learning curve, because it is an unusual library. But if you're going to spend more than a day or two with C++, it's worth learning properly.

And no, I'm not following your answer format, because I thought actually giving you a detailed answer would be more helpful. ;)

Edit:

It'd be tempting to say that an advantage of rolling your own is that you'd learn more of the language, and maybe even why the STL is one of its saving graces.. But I'm not really convinced it's true. It might work, but it can backfire too.

As I said above, it's easy to write C++ code that seems to work. And when it stops working, it's easy to rearrange a few things, like the declaration order of variables, or insert a bit of padding in a class, to make it seemingly work again. What would you learn from that? Would that teach you how to write better C++? Perhaps. But most likely, it'd just teach you that "C++ sucks". Would it teach you how to use the STL? Definitely not. A more useful approach might be utilizing the awesome power of StackOverflow in learning STL the right way. :)

这篇关于好主意/坏主意我应该重新实现大多数C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆