Function Manipulator in C++

Function Manipulators in C++ are used to manipulate/modifies the input and output stream without changing the value of that variable i.e. the value that is stored in that variable will not be changed, it is just modified/formatted on the time of execution to perform the given operations like 'printing' or 'storing that modified data', etc. Function Manipulators only modifies the I/O stream using insertion(<<) and extraction(>>) operators.

Function Manipulator


There are a few Function Manipulators that are used generally:-


  1. ends

    It is used to insert the NULL character(\0). It is defined in the ostream Header-file. It has no arguments.
    #include<iostream>
    int main( )
    {
    std::cout<<"Hello"<<"World!"; //O\P-1
    std::cout<<"Hello"<<std::ends<<"World!"; //O\P-2
    //either use "using namespace std" or std::
    }
    
    O/P-1-> HelloWorld!
    O/P-2-> Hello World!
    
  2. endl

    It is used to skip a line or break a line, it works similar to '\n'. It is defined in the ostream Header-file. It has no arguments.
    #include<iostream>
    int main( )
    {
    std::cout<<"Hello"<<"World!"; //O\P-1
    std::cout<<"Hello"<<std::endl<<"World!"; //O\P-2
    //either use "using namespace std" or std::
    }
    O/P-1-> HelloWorld!
    O/P-2-> Hello
            World!
    
  3. skipws

    It is used to skip the  "white spaces" in "istringstream". It is defined in the istream Header-file. It also has no arguments.
    #include<iostream>
    #include<sstream>
    int main( )
    {std::istringstream s("  7 92 3456");
    char a,b,c,d,e,f,g;
    s>>std::skipws>>a>>b>>c>>d>>e>>f>>g; //O\P-1
    std::cout<<a<<b<<c<<d<<e<<f<<g;
    //either use "using namespace std" or std::
    }
    O/P-1-> 7923456
    
  4. setw(value)

    It is used to set the width between the input/output. It has an argument which defines that how much width will be provided between two consecutive outputs. It is defined in the iomanip Header-file.
    #include<iostream>
    #include<iomanip>
    int main( )
    {
    std::cout<<"Hello"<<"World"; //O\P-1
    std::cout<<"Hello"<<std::setw(10)<<"World!"; //O\P-2
    //either use "using namespace std" or std::
    }
    
    O/P-1-> HelloWorld!
    O/P-2-> Hello    World!
    
  5. setfill(value)

    It is used to to fill a desired 'character' in the input/output stream. It only fills "spaces" which was created by "setw( )". It has an argument in which we can set the desired character that we want to fill-up. Its is defined in the istream Header-file.
    #include<iostream>
    #include<iomanip>
    int main( )
    {
    std::cout<<"Hello"<<"World"; //O\P-1
    std::cout<<"Hello"<<std::setw(10)<<std::setfill('#')<<"World"; //O\P-2
    //either use "using namespace std" or std::
    }
    
    O/P-1-> HelloWorld!
    O/P-2-> Hello#####World!
    
  6. setprecision(value)

    It is used restricts the number of digits of floating value. It has an arguments from which we can decided the restricted value. It is defined in the iomanip Header-file.
    #include<iostream>
    #include<iomanip>
    int main( )
    {
    float a=18.23895; 
    std::cout<<a; //O\P-1
    std::cout<<std::setprecision(3)<<a; //O\P-2
    //either use "using namespace std" or std::
    }
    
    O/P-1-> 18.23895
    O/P-2-> 18.2
    
  7. boolalpha

    It is used to set the boolean value '0' & '1' to "false" & "true" respectively. It has no argument and it is defined in the iostream Header-file.
    #include<iostream>
    int main( )
    {
    bool str1=0;
    bool str2=1;
    std::cout<<str1<<str2; //O\P-1
    std::cout<<std::boolalpha<<str1<<std::boolalpha<<str2; //O\P-2
    //either use "using namespace std" or std::
    }
    
    O/P-1-> 01
    O/P-2-> flasetrue
    





Introduction to Object-Oriented Programming language.

Introduction to C++.

For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...

Post a Comment

0 Comments