Hello there,

I am trying to figure out the advantages of C++ STL containers over C.

Correct me if I am wrong.
a)in C++ containers classes are standardized. C doesn't have any standardized libraries for data structures beyond the native array type.
b)each of these containers in C++ has member functions which can manipulate the data stored in the containers.
c)also there are global functions (algorithms) which can manipulate the data of the containers. Doesn't C has global functions as well? (reverse)
d)They're type safe and relatively clean. C offers essentially two ways to create generic containers: pointers to void (which aren't type safe) and the preprocessor (type safe, but a maintenance nightmare). C++'s templates, while still complex, are a huge improvement.

I can think or find anything else, please tell me if some of the above are incorrect or add some other advantages.

Cheers

That's a pretty good analysis. In general, you should always favor C++ containers over C ones if you have access to them.

Also, a bit random, but one of the biggest false arguments in favor of native C arrays over C++ containers is that some collection of data must be stored contiguously in memory. However, you can always use an std::vector in place of a C array. std::vector s are guaranteed to store their data contiguously in memory, and you can pass &vec[0] to any interface requiring a pointer to such a contiguous array.

I'd say 'a' is the most important. 'b' is really part of 'a'. 'c' is probably referring to the functions in <algorithm>, which C certainly does not have. 'd' is correct, void pointers and preprocessor directives are nightmares!

Dave

thanks you Ariste and Dave,

your reply was very fast and helpful. I am day before my programming exam ;)

Cheers
Christos

I am trying to figure out the advantages of C++ STL containers over C.

C++ has STL containers and C doesn't. Case closed, Sherlock. I get the impression that you're building a fallacious argument about C++'s superiority over C, which is a complete waste of time because it lacks objectivity and useful conclusions. A far more enlightening comparison would be the support in each language for writing container libraries. But that involves a great deal more effort on your part in coming up with excellent examples for each language and compelling arguments for both sides. I have yet to see such a comparison, so if you can pull it off you'll have my respect.

C++ has STL containers and C doesn't. Case closed, Sherlock. I get the impression that you're building a fallacious argument about C++'s superiority over C, which is a complete waste of time because it lacks objectivity and useful conclusions.

I am not trying to build anything, Dr.Watson. Its my tutor's question from last year's exam paper "The C++ Standard Template Library (STL) contains a number of container data types, explain with suitable examples the advantages these give to the programmer over a procedural language such as C"

Personally, I don't care at all which language is better or not. In our job (CG) there is only C++.

I am not trying to build anything

You're trying to answer a question that's clearly biased and completely nonsensical. It should be obvious that any answer you can come up with besides "what a stupid-ass question!" will also be biased and nonsensical.

"The C++ Standard Template Library (STL) contains a number of container data types, explain with suitable examples the advantages these give to the programmer over a procedural language such as C"

If you're comparing the standard C library to the standard C++ library, fine. If you're comparing C libraries in general to C++ libraries in general, fine. If you're comparing the merits of OOP to procedural programming, fine. If you're comparing C and C++ as languages, fine.

But taking the "advantages" of a library from C++ and using them to build an argument against a programming methodology? I certainly hope I'm not the only one who can see that this is a ridiculous leading question. It's practically dragging you to a mindless answer of "STL use objects, procedural languages suk, hahaha!!".

you look like you have some obsession or something, relax.

There is no point in this disagreement because is not related to the thread.
This is a question (stupid or not) based on an exam paper. There are two choices, you answer and gets the marks or you are not and fail.
Your obsession about how wrong is this question has nothing to do with me or with my thread, maybe I have to give you my tutor's email who created the question. hahaha

By me, there is not care at all about the question, if it's wrong or not. I am just trying to figure out the best possible answer. If you feel uncomfortable in this thread there is no reason keep posting.

Cheers

you look like you have some obsession or something, relax.

Yes, I'm obsessed with helping people excel at programming. As you can probably imagine, seeing idiot teachers kill off the competence of the next generation irks me to no end.

There are two choices, you answer and gets the marks or you are not and fail.

Attitudes like this are the reason why so many people graduate from school and are completely incompetent, and certainly why Turbo C++ is still the dominant compiler in universities that still teach C++. If you're too scared to challenge the system, it'll never improve. And neither will you.

>> why Turbo C++ is still the dominant compiler in universities that still teach C++

[citation?]

And, if you are using C++, then definitely use C++ containers. else if you are using C, then don't use C++ containers , lol.

Edited 6 Years Ago by firstPerson: n/a

Personally agree with Narue that the question is misleading. The C++ STL and C's procedural programing have there advantages and disadvantages. I started off writing programs in basic on my TI85 so the first language I learned was a procedural language. Then I moved on to C++ and found a whole new world of options. Now I blend the two to suite my needs. The really important thing to learn is how to use both styles and then what style to use to solve a particular problem. If you get in the mindset that C++ must be better so therefore I will code in only C++ then you will be heading for trouble. Just because one can doesn't mean one should.

This article has been dead for over six months. Start a new discussion instead.