Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it Bad Practice to use C++ only for the STL containers?

Tags:

c++

c

containers

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?

like image 408
ldog Avatar asked Jan 29 '10 05:01

ldog


People also ask

Can we use STL in C?

C can't have an "exact equivalent" of STL because C doesn't have templates or classes. You might be interested in the "Glib collections" library: http://www.ibm.com/developerworks/linux/tutorials/l-glib/

Why should a C++ programmer be interested in the STL?

Why should a C++ programmer be interested in the STL? Because the STL embodies the concept of reusable software components, and provides off-the-shelf solutions to a wide variety of programming problems.

Is STL container thread safe?

The SGI implementation of STL is thread-safe only in the sense that simultaneous accesses to distinct containers are safe, and simultaneous read accesses to to shared containers are safe.

Is STL important in C++?

STL provides a range of data structures that are very useful in various scenarios. A lot of data structures are based on real-life applications. It is a library of container classes, algorithms, and iterators. It is a generalized library and so, its components are parameterized.


2 Answers

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).

like image 157
bobbymcr Avatar answered Oct 05 '22 01:10

bobbymcr


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

like image 37
Ignacio Vazquez-Abrams Avatar answered Oct 04 '22 23:10

Ignacio Vazquez-Abrams