Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ standard library - when should I use it and when shouldn't I?

I was wondering how often people actually use much of the standard c++ library, particularly the stuff in the <algorithm> and <numeric> headers. The text books seem to recommend them, but I haven't seen any of them used at all in various projects I've sifted through (coincidence?) and personally it seems easier to just write appropriate simple algorithms myself each time rather than memorize or consult a reference to these headers each time. Am just being lazy or stubborn? Is there actually performance gains etc when using these libraries?

Thanks,

R

like image 504
Russel Avatar asked Jan 21 '11 22:01

Russel


People also ask

Why do we use standard library in C?

The C standard library provides macros, type definitions and functions for tasks such as string handling, mathematical computations, input/output processing, memory management, and several other operating system services.

How does the C standard library work?

C Standard library functions or simply C Library functions are inbuilt functions in C programming. The prototype and data definitions of these functions are present in their respective header files. To use these functions we need to include the header file in our program.

What happens if standard library is not present in C?

Without the standard library, you're entire reliant on your own code, any non-standard libraries that might be available to you, and any operating system system calls that you might be able to interface to (which might be considered non-standard library calls).

Why do we need standard library in C++?

The C++ Standard Library provides several generic containers, functions to use and manipulate these containers, function objects, generic strings and streams (including interactive and file I/O), support for some language features, and functions for everyday tasks such as finding the square root of a number.


2 Answers

It's possible you're being lazy or stubborn. Personally, I use them all the time in production code.

I don't do this to be fancy, and I don't do this because I like writing "space-age code." Rather, I do this because I am a paranoid programmer, and I know that production environments are hostile places that will mutilate code and reduce my programs to smoking piles of worthless bytes, if given a chance.

I do this because I live by the motto, "The best code, is the code you never write." It takes time to learn how to use the STL & Std Lib effectively, but once you do you'll find that it can be used so that what now is 1000 lines of code becomes perhaps 100. Those 100 might take as long to write as the original 1000, but there are fewer failure points. The code can be more robust, if you stand on the shoulders of others.

like image 147
John Dibling Avatar answered Oct 05 '22 22:10

John Dibling


You should use stl as much as possible.
It has been written by pretty sophisticated programmers and it is very unlikely that you can write a more optimized version of any of stl stuff.
Do not re-invent the wheel

like image 24
Cratylus Avatar answered Oct 05 '22 23:10

Cratylus