Basic of C++ Standard Library

Discussion in 'C++' started by BiplabKamal, May 26, 2016.

  1. C++ standard library is a set of user defined types(classes), objects and functions which help to develop applications. Without standard library it is very very difficult to write a useful application. Standard library is an integrated part of the language and the language specification defines the specification for the standard library also. Standard Library is part of C++ ISO standard. C++ ISO standard also defines the performance criteria of the Standard Library. Most of the compiler developers provide the standard library along with the compiler.

    Core C++ language capability is limited to processing data in the memory. It is all about data and operations on the data. There are inbuilt data types(primitive types) and operations (operators). You can also create new data type and operations which are called user defined types and operations. When you write a function you are actually creating a new operation. By using ‘struct’, ‘class’, ‘union’ and ‘enum’ key words you can create new types which include data as well as operations. User defined types and operations are using the inbuilt types and operations or other user defined types and operations.

    Any program is not useful if it cannot interact with input-output devices like Hard disk, Monitor, Internet, Printer, Scanner, Keyboard, Mouse and so on. But have you noticed that you cannot interact with those input output devices with inbuilt data types and operations? This is because those operations need to access hardware resources which are managed by the underlying operating system. Operating System does not allow a user mode program to access the system resources directly. Instead the OS provides some user defined operations and data types to request the OS for access the system resources. They are called OS API (Application Programming Interface). API not only provide access to input-output resources but also provide access to other resources(Threads, Memory etc.) manged by the OS. Normally system API is a set of low level functions. Without standaard library and using OS API, writing a program will need to implement a lot of logic and code. Standard library provide the high level functions and objects for easy access to OS API. For example look at following code which will display “Hello World” on the console window in Windows OS.
    #include<windows.h>//Windows API functions are declared in this file
    int main()
        DWORD NumberOfCharsWritten{ 0 };
        char lpBuffer[]{ "Hello World\n" };
        int size = sizeof(lpBuffer);
        HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // Get the window handle of the console window
        BOOL bRet = WriteConsole(console,lpBuffer,size,&NumberOfCharsWritten,NULL); // Write to the console
    The disadvantage of the above code is that it will compile and run only on Windows operating system. You have to use different API for linux or other operating systems. More over the WriteConsole() function only accept to character buffer, you cannot use other data types. If you want to an integer number you have to first convert it to character and write to console. Also you may need to set the code page of the console before writing to it. Now look at the modified code using standard library function:
    #include<stdio.h>//printf() and other input output functions are declared in this file
    int main()
        printf("Hello World\n");
    The above code is simpler and cleaner. Also the same code will get compiled and run in other OS. You can pass any inbuilt data types as arguments:
    #include<stdio.h> //printf() function is declared in this file
    int main(int argc, char *argv[])
        printf("Hello World.\n");
        printf("The executable file name is %s\n", argv[0]);
        printf("Number of command line argument is %d\n",argc-1);
    Compile the above code and run using following commands:

    clang standardlibrary.cpp -o standardlibrary.exe
    standardlibrary.exe abcd xyz

    You will get following output:

    Hello World.

    The executable file name is standardlibrary.exe

    Number of command line argument is 2

    Now the printf function is a C style function and not enough high level. C++ standard library also provides classes to handle robust way of input output operations. Look at the following function using C++ input output objects:
    #include<iostream> // standard library classes and objects are declared in this file
    int main(int argc, char *argv[])
        std::cout<<"Hellow World.\n";
        std::cout<<"The executable file name is "<<argv[0]<<std::endl;
        std::cout<<"Number of command line argument is "<<argc-1<<std::endl;
    To compile above function you may need to use -fms-compatibility-version=19.00 option like:

    clang ..\standardlibrary.cpp -fms-compatibility-version=19.00 -o standardlibrary.exe

    Other than handling system resources there are lot of reusable code included in the standard library which are frequently used by programmers. The rich set of functionality of the standard library helps the programmers to develop the application very fast, reducing their effort to make critical design decision on optimum algorithms.

    So basically the Standard Library provides benefits to the C++ developer community in two areas:

    1 Abstracting the Operating System functions

    Abstracting or hiding the OS functionality helps the programmer to write OS independent code, which can be compiled for any target OS. OS independent programming is the most discussed feature among developer community. Some languages like Java and C# tried to achieve this objective by using a run-time layer which takes care of OS functionality. C++ achieves this with it’s standard library. The difference between run-time and standard library is that in case of standard library the application need to be compiled in every OS which is not true for run-time based applications. But standard library based application does not have the overhead of run-time framework.

    2 Ready to use critical Data Structures, Algorithms and Utility functions

    There are lot of data structures and algorithms which programmers might be using frequently. Having those as part of standard library helps the programmer from the burden of reimplementing them. Standard library has a lot of classes to handle data structures like queue, list and map and algorithm like sorting and searching. Without standard library those data structures and algorithms will be implemented by the programmers which will defeat the purpose of code re-usability. Also by having a single version it keeps on improving the performance as the experience and research of the developer community helps to improve them.

    It has classes and functions for handling strings, localization support, numerical functions, diagnostics and many more utilities. Let us see some examples:

    Example: String handling using std::string class

    #include <iostream>
    #include <string>
    struct Item
        std::string Name;
        int Price;
        std::string Seller;
    void PrintItem(Item it)
        std::cout << it.Name + " is sold by " + it.Seller + " at Rs." << it.Price<<std::endl;
    int main() 
        Item item1{"Rice",75.50,"Himalaya Grain"};
        return 0;
    Standard library is an integral part of of the C++ language. Still all vendors may not support the full feature as per C++ standard specification. You should read the library documentation to know their compatibility level. The latest version of C++ releases is C++14 and C++17 is targeted to be released in 2017. It is better to use the standard library which support the latest language specification. To use a function or class from the library you need to know three things: Location of the library, name of the header which declare class or function and the namespace (package of class and functions) the class or function belongs to.

    We will discuss the details of standard library in the next part of the tutorial.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice