使用 C++ 是不好的做法吗?只适用于STL容器?

发布于 2024-08-19 22:19:37 字数 781 浏览 6 评论 0原文

首先是一些背景知识......

接下来,我使用 C、C++ 和 Java 来编码(通用)算法,不是 GUI 和带有界面的花哨程序,而是简单的命令行算法和库。

我开始学习 Java 编程。我对 Java 非常熟悉,并且学会了如何使用 Java 容器,因为它们往往会降低簿记的复杂性,同时保证出色的性能。我断断续续地使用过C++,但我对它的使用绝对不如Java,而且感觉很麻烦。我对 C++ 的了解不够,无法在不需要查找每个函数的情况下使用它,所以我很快又回到尽可能多地坚持使用 Java。

然后我突然转向汇编语言的破解和黑客攻击,因为我觉得我把太多的注意力集中在一种太高级的语言上,并且我需要更多关于 CPU 如何与内存交互以及 1 的真正情况的经验和 0。我不得不承认,这是迄今为止我使用计算机时最有教育意义和最有趣的经历之一。

由于明显的原因,我无法每天使用汇编语言进行编码,它主要是为了消遣而保留的。通过这次经历对计算机有了更多的了解后,我意识到 C++ 比 Java 更接近于“1 和 0 的层次”,但我仍然觉得它非常迟钝,就像一把瑞士军刀,上面有太多的数字。优雅地完成任何一项任务的小发明。我决定尝试一下普通的香草 C,我很快就爱上了它。这是简单性和足够的“微观管理”之间的一个愉快的媒介,而不是抽象出真正发生的事情。然而,我确实错过了关于 Java 的一件事:容器。特别是,一个可以动态扩展大小的简单容器(如 stl 向量)非常有用,但每次都必须在 C 中实现非常痛苦。因此,我的代码目前看起来几乎完全是 C,其中添加了 C++ 的容器,这是我使用 C++ 的唯一功能。

我想知道在实践中只使用 C++ 的一项功能并忽略其余功能以支持 C 类型代码是否可以?

First a little background ...

In what follows, I use C,C++ and Java for coding (general) algorithms, not gui's and fancy program's with interfaces, but simple command line algorithms and libraries.

I started out learning about programming in Java. I got pretty good with Java and I learned to use the Java containers a lot as they tend to reduce complexity of book keeping while guaranteeing great performance. I intermittently used C++, but I was definitely not as good with it as with Java and it felt cumbersome. I did not know C++ enough to work in it without having to look up every single function and so I quickly reverted back to sticking to Java as much as possible.

I then made a sudden transition into cracking and hacking in assembly language, because I felt I was concentrated too much attention on a much too high level language and I needed more experience with how a CPU interacts with memory and whats really going on with the 1's and 0's. I have to admit this was one of the most educational and fun experiences I've had with computers to date.

For obviously reasons, I could not use assembly language to code on a daily basis, it was mostly reserved for fun diversions. After learning more about the computer through this experience I then realized that C++ is so much closer to the "level of 1's and 0's" than Java was, but I still felt it to be incredibly obtuse, like a swiss army knife with far too many gizmos to do any one task with elegance. I decided to give plain vanilla C a try, and I quickly fell in love. It was a happy medium between simplicity and enough "micromanagent" to not abstract what is really going on. However, I did miss one thing about Java: the containers. In particular, a simple container (like the stl vector) that expands dynamically in size is incredibly useful, but quite a pain to have to implement in C every time. Hence my code currently looks like almost entirely C with containers from C++ thrown in, the only feature I use from C++.

I'd like to know if its consider okay in practice to use just one feature of C++, and ignore the rest in favor of C type code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

却一份温柔 2024-08-26 22:19:37

简短的回答是:“这实际上并不是使用 C++ 的最有效方法。”

如果使用得当,强类型系统、通过引用传递的能力以及 RAII 之类的习惯用法将使 C++ 程序更有可能是正确的、可读的和可维护的。

没有人可以阻止您按照自己想要的方式使用该语言。但您可能会因为不学习和利用实际的 C++ 功能而限制自己。

如果您编写其他人必须阅读和维护的代码,他们可能会欣赏使用“真正的 C++”而不是“带有类的 C”(用之前的评论者的话来说)。

The short answer is, "This is not really the most effective way to use C++."

When used correctly, the strong type system, the ability to pass by reference, and idioms like RAII make C++ programs more likely to be correct, readable, and maintainable.

No one can stop you from using the language the way you want to. But you may be limiting yourself by not learning and leveraging actual C++ features.

If you write code that other people will have to read and maintain, they will probably appreciate the use of "real C++" instead of "C with classes" (in the words of a previous commenter).

初心未许 2024-08-26 22:19:37

对我来说似乎不错。这也是我真正使用的 C++ 的唯一部分。

Seems fine to me. That's the only part of C++ that I really use as well.

倚栏听风 2024-08-26 22:19:37

现在,我正在写一个数字计算器。没有多态性,没有控制委托,没有交互。 是一个瓶颈,因此我用 C 重写了 I/O。

这些函数大部分位于代表工作线程的一个类中。因此,与其说是面向对象,不如说是线程局部变量。

除了矢量之外,我还大量使用。但重型数据结构是用纯 C 编写的。主要是循环单链表,甚至不能轻易地具有不同的 begin()end(),这意味着不仅容器而且序列(和 for 循环)都是禁止的。然后模板帮助预处理器生成主内循环。

解决问题的最自然的方法可能是正确的。你不想为了寻找问题而寻求解决方案。学习使用 C++ 很好,但是面向对象只适合某些问题,而不适合其他问题。

另一方面,在 C++ 程序中使用 stdlib.h 中的 bsearch 是错误的。

Right now, I'm writing a number cruncher. There's no polymorphism, no control delegation, no interaction. <iostream> was a bottleneck so I rewrote I/O in C.

The functions are mostly inside one class which represents a work thread. So that's not so much OO as having thread-local variables.

As well as vector, I use <algorithms> pretty heavily. But the heavy-duty data structures are written in plain C. Mainly circular singly-linked lists, which can't even easily have distinct begin() and end(), meaning not only containers but sequences (and for-loops) are off-limits. And then templates help the preprocessor to generate the main inner loop.

The most natural way of solving your problem is probably right. You don't want solutions in search of a problem. Learning to use C++ is well and good, but object orientation is suited to some problems and not others.

On the other hand, using bsearch from stdlib.h in a C++ program would be wrong.

方觉久 2024-08-26 22:19:37

您应该以对您最有意义的任何方式使用 C++。

You should use C++ in whatever way makes the most sense for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文