After going through all the functions and statements of C++ you might be feeling tired. Well, it’s obvious to get a bit lazy on the way. But to get you actively work on your programs, C++ has several built-in libraries, so called standard libraries. You know, there lots of pre-defined functions in the classes of the standard libraries to save your time on writing such complex functions. What more a wise programmer will be willing to get other than such pre-defined function especially if they work perfectly.
So, in these sections I have kept some useful standard library classes before you, So, you get to know what are the things lagging in your program and what more functions you can add. This also helps you to get better program ideas for the next project.

  • Vectors Standard Library:

    You have been using array till I guess. But, you might have found some problems using it. Due to lack of some functions and being static, it lags with your work. So, a standard library known as the vectors is there to solve most of your problem. A Vector is a kind of dynamic array which can grow and shrink throughout the program and have all the features of an array (even more). Though, there is a little problem over arrays is that it takes a bit more memory than an array and may cause performance issues if the vector is made quite large.
    So here’s how to declare a vector:

    1. First, you need to import the vector library class using this code: #include
    2. Now to define a vector variable just follow the given code => vector<datatype> vectorname;
      For example: I am defining a vector which carries character value and is named as “myCharVecor”..
      vector<char> myCharVector;
    3. To insert an element just use the .push_back() function, which will add an element to its end.
      myCharVector.push_back('A');
    4. Similarly, you can remove the last element using the .pop_back() function.
      myCharVector.pop_back();
      To remove all the elements of the vector in one step just use .clear() function.
      myCharVector.clear();
    5. Oh, and you can also set the number of elements initially and can give a default value to them while declaring.
      vector<char> myCharVector(5,'X');
      It will create a vector of 5 elements and the value of the element will be ‘X’.
    6. Above the length of the vector was of 5 elements, which can be called its vector size. You can also find the size of the vector in the program just by using the .size() function.
      myCharVector.size();
    7. Though, there are lots more function in the vector standard library class, which you can tinker yourself, but here I we are going to get a small glance of multiple classes. Here’s a code for a program to print all the vowels stored in vectors.

      #include<iostream>
      using namespace std;
      #include<vector>
      int main()
      {
      vector<char> vowels;
      vowels.push_back('a');
      vowels.push_back('e');
      vowels.push_back('i');
      vowels.push_back('o');
      vowels.push_back('u');
      for(int i=0;i<vowels.size();i++)
      {
      cout<<vowels[i]<<endl;
      }
      return 0;
      }

      You can write the above in another way too:

      #include<iostream>
      using namespace std;
      #include<vector>
      int main()
      {
      vector<char> vowels(5); //Declares the size initially by 5 elements
      vowels[0]='a';
      vowels[1]='e';
      vowels[2]='i';
      vowels[3]='o';
      vowels[4]='u';
      for(int i=0;i<vowels.size();i++)
      {
      cout<<vowels[i]<<endl;
      }
      return 0;
      }
  • Algorithms Standard Library:

    This standard library contains a bunch functions to manipulate elements of a vector. Like if we want to check if some element exists in the vector, or we want to search an element’s position in a vector. And much more, including deleting an element, sorting the elements, shuffling the elements, etc.

    1. To include the algorithm files type this:
      #include<algorithm>
      Plus
      using namespace std;
      As it is a standard library or you can use std:: before every function.
    2. I will show you one example of how to know if a vector contains a specific element.
      First, let us get to know about the iterators, which works much like a pointer for vectors. We need to declare an iterator like this:

      vector<datatype>::iterator itername;

      For example:

      vector<char>::iterator iterate;

      This will declare an iterator. And now you can use it like a pointer like this:

      int main()
      {
      vector<char> vowels('a',5);
      vector<char>::iterator iterate;
      iterate=vowels.begin();
      *(iterate+1)='e';
      cout<<vowels[1];
      return 0;
      }
      Now we will use the find() function for the purpose of checking if some element is in the vector.

      #include<iostream>
      using namespace std;
      #include<vector>
      #include<algorithm>
      int main()
      {
      vector<char> vowels;
      vector<char>::iterator iterate;
      vowels.push_back('a');
      vowels.push_back('e');
      vowels.push_back('i');
      vowels.push_back('o');
      vowels.push_back('u');
      iterate=find(vowels.begin(),vowels.end(),'u');
      if(iterate!=vowels.end())
      {
      cout<<"Element found.\n";
      }
      else
      {
      cout<<"Element not found.\n";
      }
      return 0;
      }
  • String Standard Library:

    After starting C++ and getting through declaring variables, you might be facing problems while declaring a string variable. Then you may have learned a way to declare a string value using char, which is simply using an array of characters and made it too much complex for manipulating the usage of it with string.h library functions. Plus, it lacks flexibility, especially when you want to use it all like other datatypes. With other datatypes like int or char, you can simple use them as return types, or create an array with it and much more stuffs. Well, using the string module which I am gonna introduce is also simple like with char and int datatype.
    So, here I am introducing you with the standard library, especially for strings, which let you declare a string variable and use it like the variables of other data types.

    1. To include the string standard library do the same as you do with other classes:

      #include<string>
    2. Declaring a string is also same as others,

      string variablename;
    3. Declaring a string array,
      string arrayname[size];
    4. A function of return type string:

      string functionaname(){
      return string string variable;
      }
    5. There several functions too to manipulate or work on string data. Some are finding the length, finding the size, searching a character, adding characters, changing characters etc of a string are there.
      For example to find the length of string just use .length() function,
      string stringname;
      cout<<stringname.length();

      -Here stringname.length() will return the length of the string.

    I recommend this basic library of string to use for working with string values.

  • Time.h Standard library:

    This standard library contains the function related to setting and manipulating time and date.
    To include the library just add these lines, #include<time.h>. Some examples of usage are:
    First, you need to have an object to work with time. Declare it like below:
    time_t ti; //Here ti can be changed with any name
    The code to print current time and date is:

    time_t ti;
    time(&ti); //Fill ti with current time
    cout<<ctime(&ti); //Print the current time

  • File Streaming function:

    After working with iostream header, you should know to use the fstream header. This header is useful when streaming through files in the system. This header contains the class ifstream,ofstream and fstream.
    ifstream is used to get input from a file, ofstream to output to a file. And fstream can be used for both purposes.

    1. Here’s to how to open a file:

      fstream file("ExampleFile.txt");

      Or

      fstream file;
      file.open();
    2. To get the words’ list as an input from file. Assume that there is a text file containing some text. Say the text file is “codegeek.txt” and contains a text like this:
      Codegeek is the best blog
      The code will be like this(including the header files):
      #include<iostream>
      using namespace std;
      #include<fstream>
      #include<string>
      int main()
      {
      ifstream file("codegeek.txt"); //Opening the file and entering the file path
      string word;
      while(file) //Running the file until the file doesn't end
      {
      file>>word; //Getting string inputs from the file
      cout<<word<<endl; //Printing the words
      }
      file.close(); //File should be closed after usage
      return 0;
      }

      Output:
      Codegeek
      is
      the
      best
      blog
    3. To write something to a file follow next:

      int main()
      {
      ofstream file("codegeek.txt"); //Opening a file in writing mode
      string text="I am writing in a text file.";
      file<<text; //Inserting text to the file
      file.close();
      return 0;
      }

      Now check the file and the old test will be overwritten by the new text.
    4. You can also add new text to a file without overwriting it by using the append mode.

      int main()
      {
      ofstream file("codegeek.txt",ios::app); //ios::app is used to set appending mode
      string text="Writing is being continued";
      file<<text;
      file.close();
      return 0;
      }

    There are several other modes which you can use like working with binary files,writing on conditions etc.

I hope you found what you were searching for or even more. Things will get easier with these libraries.
So best of luck with your coding and don’t forget to share if you liked it. 🙂