Skip to main content

1.5 Getting Deeper into the C++ Language (Libraries, Strings, I/O and other pre-defined function)

Now, as we’ve previously discussed as well, In any programming Language there are libraries of functions which one may use to write programs. Libraries contain functions which someone has already written for us to use and to increase productivity.

Some of the most useful libraries and functions in those libraries in C++ are:

1) The <string> library:

As previously discussed, Strings are one of the most integral parts of any programming language.
Strings are series of Characters stored one after another in memory, We can use strings to store names, addresses, a story or any other thing we want.

Once, We’ve included the <string> library, we could do a host of things in C++, we can add other characters to the strings, we can add strings to strings or any other thing we may possibly want.
Some of the most useful and common functions of the string library are:

i) the ‘+’ operator:
This is an operator which could be used to add the contents of two strings

ii) the size function:
This function could be used to find the size or number of characters in a string.

iii) the compare function:
This could be used to compare two strings and say if they are the same or if part of them is the same.

iv) the find function:
This could be used to find the location of any particular character in the string.

The following example provides a solid example for all the functions discussed above:

#include <iostream>
#include <string>
using namespace std;
int main(){
            string name;
            cout<<"Pleas Enter your Name?"<<endl;
            cin>>name;
            cout<<"It seems your name has:"<<name.size()<<" characters."<<endl;
            string print = "Hello, "+name;
            print = print + '!';
            cout<<print<<endl;
            if(name.compare("Shubh")==0){
                        cout<<"Hello, Sir!"<<endl;
            }

            size_t pos = name.find('u');
            if(pos != string::npos){
                        cout<<"It seems like your name consists of 'u' as well! "<<endl;
            }
}


NOTE: size_t is another data type which is used to store variables which have something to do with the size or position in strings. and string::npos is another way of saying that a particular character doesn’t exist in a string

You may try running the code yourself as well.

ii) The <algorithm> Library

The Algorithm library consists of various functions pertaining to methods of doing something in code. It has functions with which we may sort an array(put its characters in ascending/descending order), find the maximum or minimum in any array, check if its sorted or if it’s a permutation of any other list of numbers/characters, or search for any other number is a sorted/unsorted array of numbers.

You may find almost everything part of the algorithm library at: https://goo.gl/h8gfhy. Be sure to go through the sort function, search function among others.

iii) The <vector> Library

A Vector in C++, is an array with a variable length, it does have a certain given data type, but its length could be changed, unlike an array which has a fixed length.

The Vector library contains of functions which are used to deal with vectos in C++.

Some of its common functions are:

i) begin : To get what’s known as an ‘iterator’ to the beginning of any particular vector.

ii) end: To get an iterator to the end of any particular vector.

iii) push_back : To add new items/elements into a vector

iv) size: To know the size of the vector

v) the [] operator: To know of the element in any particular index(spot) in a vector

NOTE: An iterator is like an index to an array, it is like a position(in a manner of speaking!), there’s another <iterator> library available in C++ to manipulate iterators to do something/anything.

An example demonstrating all of the above functions is the following:
#include <iostream>
#include <vector>

using namespace std;

int main(){
            int temp;
            vector<int> numbers;
            cout<<"Please enter about 50 numbers."<<endl;
            for(int i = 0;i<50;i++){
                        cin>>temp;
                        numbers.push_back(temp);
            }
            cout<<"The Vector has "<<numbers.size()<<" numbers."<<endl;
            cout<<"The Number at the 23rd position is:"<<numbers[23-1]<<endl;
            cout<<"The Number at 8th position is: "<<numbers[8-1]<<endl;

}

NOTE: We have 23-1 to get the 23rd member of the vector and 8-1 to get the 8th member of the vector because in any vector/array, The Numbers are 0-indexed, that is their indexing or numbering starts from 0. The first character is accessed by [0], the second character by [1] and so on….


Also, Notice the way we declare a vector, its as vector<int> numbers, so the general syntax of declaring any vector is: vector<’data type’>’vector name’;


There are other containers available in C++ as well.


There is what we call a set, which is simply an array, which is already sorted in ascending order for our own convenience. Sets are included with the <set> library. You many find more about sets and its functions at : 


Please remember that you need not to remember all these things always, whenever you’d program you’ll have a list of functions and libraries with you as documentation and you could refer to it anytime you want, and with practice these things would catch a place in your mind on their own.

In C and C++, We comment on our code, to make it convenient for other developers/programmers to know why we did, what we did, Comments don’t affect the working/compiling/executing of any program they are simply a means to tell other human beings about what you meant by writing some piece of code.

You may comment on code as follows by using ‘//’ marks before the comment. Eg:

#include <iostream>

using namespace std;
int main(){
            int number;                               //Declaring a number
            cin>>number;                            //Taking input into the number
            cout<<number<<endl;                 //Printing the Number
}


Here you notice that whatever I’ve written after the double slashes ‘//’ won’t affect the working of my code but would just tell anyone reading it about why I did so.


Exercise


Try implementing code with all the libraries discussed above!

Comments