Is it wrong to use C ++ only for STL containers?

First, a little background ...

In the future, I use C, C ++ and Java to encode (general) algorithms, not a GUI and a fantasy program with interfaces, but simple command line algorithms and libraries.

I started learning Java programming. I did a great job with Java, and I learned to use Java containers a lot, as they tend to reduce the complexity of accounting, guaranteeing excellent performance. I used C ++ periodically, but I was definitely not as good with it as with Java, and it seemed cumbersome. I did not know that C ++ was enough to work in it without looking at every function, and therefore I quickly returned to sticking to Java as much as possible.

Then I suddenly turned to hacking and hacking in assembly language because I felt that I was too focused on a too high level of language, and I needed more experience working with a processor that interacts with memory, and what really happens with 1 and 0. I must admit that this was one of the most educational and interesting events that I have had with computers to date.

For obvious reasons, I could not use assembly language for daily use of the code, it was mainly reserved for funny sabotage. Learning more about this computer, I realized that C ++ is much closer to β€œlevel 1 and 0” than Java, but I still felt that it was incredibly dumb like a Swiss army knife with too many gizmos to make any task with elegance. I decided to try plain C vanilla and I fell in love quickly. It was a happy environment between simplicity and enough "micromoment" to not ignore what was really happening. However, I missed one thing about Java: containers. In particular, a simple container (such as a stl vector) that dynamically expands in size is incredibly useful, but it is very painful to execute in C every time. Therefore, my code currently looks almost entirely C with containers from C ++ that I have selected, the only function that I use from C ++.

I would like to know if in practice I can use only one feature of C ++ and ignore the others in favor of code like C?

+4
source share
4 answers

Short answer: "This is not the most efficient way to use C ++."

When used correctly, a strong type system, the ability to pass by reference, and idioms such as RAII make C ++ programs more correct, readable, and supported.

No one can stop you from using the language the way you want. But you can limit yourself by not learning and not using the real features of C ++.

If you write code that other people will need to read and maintain, they will probably appreciate using "real C ++" instead of "C with classes" (according to a previous commentator).

+14
source

I feel good. This is the only part of C ++ that I really use.

+6
source

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

Functions are mainly located within the same class, which is a workflow. So not so much OO as local flow variables.

Like vector , I pretty much use <algorithms> . But heavy-duty data structures are written in regular C. Basically, circular, simply-linked lists that cannot even easily have different begin() and end() , which means not only containers, but also sequences (and for-loops) of limits. And then the templates help the preprocessor generate the main inner loop.

The most natural way to solve your problem is probably right. You do not want solutions in search of a problem. Learning to use C ++ is good and good, but object orientation is suitable for some problems, not others.

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

+4
source

You should use C ++ in any way that makes the most sense to you.

+4
source

Source: https://habr.com/ru/post/1299714/


All Articles