Sunday, August 21, 2011

Using arrays we can store a group of values under a single name. We have C-style arrays in a C++ program. But C-style arrays are inflexible and in some ways awkward to use, so we will use the C++ implementation of the structure, which is a vector.
To use vectors in your code, you need the appropriate library -

Vectors are declared with the following syntax:

vector variable_name (number_of_elements);

The number of elements is optional. You could declare it like this:

vector variable_name;

And that would declare an empty vector — a vector that contains zero elements.


vector values (5); // Declares a vector of 5 integers

vector grades (20); // Declares a vector of 20 doubles

vector names; // Declares a vector of strings,
// initially empty (contains 0 strings)

After a vector has been declared specifying a certain number of elements, we can refer to individual elements in the vector using square brackets to provide a subscript or index, as shown below:


Returns the number of elements in v.

The = operator

You can use the = (assignment) operator to assign one vector to another, for example v1 = v2, so long as they are vectors of the same type (eg both vector or vector). In this case the contents of v1 are overwritten with the contents of v2 and v1 is truncated or extended to be the same length as v2.

push_back() member function
This function allows you to add elements to the end of a vector.

The pop_back() member function

This function removes the last element from the vector.Unfortunately pop_back() doesn't return the value of the popped element.

C++ strings behave (to some extent) like vectors of char

string s = "eyrie";
s[1] = s[0]; // s is now "eerie"
string t = "cat";
char ch = t[0]; // ch is the letter 'c'; note that ch is of type char, not type string
t[0] = t[1];
t[1] = ch; // t is now "act"

Vectors are good at:
  • Accessing individual elements by their position index (constant time).
  • Iterating over the elements in any order (linear time).
  • Add and remove elements from its end (constant time).

Sunday, August 7, 2011

How to change the appearance of the Bash Prompt

First of all - backup your .bashrc file
cp .bashrc .bashrc-backup

now edit .bashrc with your editor:
gedit .bashrc

in the end of the file add:




the first color is for the normal font and barckets, and the second one is for the user name and host
30: Black/Dark grey
31: Red 32: Green
33: Yellow
34: Blue
35: Magenta

36: Fuscia

37: White/light grey

38: "Default"

foreground color
in the exmaple "1;37m" you will have to change the 37 to another number.
The 1 before it can be set into zero, it is for light \ dark colors
rename the PROMPT_HOSTNAME to your hostname (or anything you want) and save the file.

You can try the options below and can select the nice colour combinations of your choice.

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\e[${PROMPT_COLOR2}\u@\h\[\033[00m\]:\[\033[1;37m\]\w\[\033[00m\]\$ '

PS1='\n\[\033[1;34m\]\342\226\210\342\226\210 \u @ \w\n\[\033[0;36m\]\342\226\210\342\226\210 \t $ \[\033[0;39m\]'

PS1="$C_BLUE[$C_RED\$?$C_BLUE][$C_RED\u@\h:\w$C_BLUE]\$ $C_RESET"

Try my choice..
if [ $(id -u) -eq 1 ];
then # you are root
PS1='\[\e[1;31m\][\u@\h \W]\$\[\e[0m\] '
else # normal


PS1='\[\033[01;34m\]\u @ \w\[\033[0;36m\]\t $ \[\033[0;39m\]♡'


You can see a calendar at the begining...The command cal run each time when the terminal opened and calendar will be displayed.You can replace cal by other command also..

unix tool awk

The Awk text-processing programming language and is a useful tool for manipulating text.

  • Awk recognizes the concepts of "file", "record", and "field".
  • A file consists of records, which by default are the lines of the file. One line becomes one record.
  • Awk operates on one record at a time.
  • A record consists of fields, which by default are separated by any number of spaces or tabs.
  • Field number 1 is accessed with $1, field 2 with $2, and so forth. $0 refers to the whole record.
  • Examples

    Print every line after erasing the 2nd field

    awk '{$2 = ""; print}' file

    Print hi 48 times
    yes | head -48 | awk '{ print "hi" }'
    print only lines of less than 65 characters
    awk 'length < 64'

    In awk, the $0 variable represents the entire current line, so print and print $0
    do exactly the same thing.

    AWK Variables

    awk variables are initialized to either zero or the empty string the first time they are used.
  • Variable declaration is not required
  • May contain any type of data, their data type may change over the life of the program
  • Must begin with a letter and continuing with letters, digits and underscores
  • Are case senstive
  • Some of the commonly used built-in variables are:

    • NR -- The current line's sequential number
    • NF -- The number of fields in the current line
    • FS -- The input field separator; defaults to whitespace and is reset by the -F command line parameter
    Integer variables can be used to refer to fields. If one field contains information about which other field is important, this script will print only the important field:
    $ awk '{imp=$1; print $imp }' calc

    The special variable NF tells you how many fields are in this record.
    This script prints the first and last field from each record, regardless
    of how many fields there are:

    Unix tool sed

    sed (stream editor) is a Unix utility that parses text and implements a programming language which can apply transformations to such text. It reads input line by line, applying the operation which has been specified via the command line, and then outputs the line.

    Sed is a powerful stream editor. Sed copies its input to its output, editing it in passing. Each portion of an input line matching a regular expression can be replaced with a fixed string or another portion of the input. Lines matching a regular expression can be deleted. GNU sed understands Unicode.


    The following example shows a typical, and the most common, use of sed,
    where the
    -e option indicates that the sed expression follows:
    sed -e 's/oldstuff/newstuff/g' inputFileName > outputFileName

    Under Unix, sed is often used as a filter in a pipeline:

    generate_data | sed -e 's/x/y/g'
    That is, generate the data, and then make the small change of
    replacing x with y.

    Several substitutions or other commands can be put together in a file.
    sed -f subst.sed inputFileName > outputFileName

    Besides substitution, other forms of simple processing are possible.
    For example, the following uses the d command to delete lines that
    are either blank or only contain spaces:

    sed -e '/^ *$/d' inputFileName

    This example used some of the following regular expression metacharacters:

    • The caret (^) matches the beginning of the line.
    • The dollar sign ($) matches the end of the line.
    • The asterisk (*) matches zero or more occurrences of the previous character.


    To delete a line containing a specific word from the file use:
    sed '/yourword/d' yourfile
    To delete only the word use:
    sed 's/yourword//g' yourfile

    Use of Unix tool "make"

    The make utility is a software engineering tool for managing and maintaining computer programs. Make provides most help when the program consists of many component files. As the number of files in the program increases, compile time and complexity of compilation command also increases.

    With make, the programmer specifies what the files are in the project and how they fit together, and then make takes care of the appropriate compile and link steps. Make can speed up your
    compiles since it is smart enough to know that if you have ten .c files but you have only
    changed one, then only that one file needs to be compiled before the link step. Make has
    some complex features, but using it for simple things is pretty easy.

    Simple Example

    This is an example descriptor file to build an executable file called prog1. It requires the source files,, and An include file, mydefs.h, is required by files and If you wanted to compile this file from the command line using C++ the command would be

      % CC -o prog1

    This command line is rather long to be entered many times as a program is developed and is prone to typing errors. A descriptor file could run the same command better by using the simple command

      % make prog1

    or if prog1 is the first target defined in the descriptor file

      % make

    Let's go through the example to see what make does by executing with the command make prog1 and assuming the program has never been compiled.

    1. make finds the target prog1 and sees that it depends on the object files file1.o file2.o file3.o
    2. make next looks to see if any of the three object files are listed as targets. They are so make looks at each target to see what it depends on. make sees that file1.o depends on the files and mydefs.h.
    3. Now make looks to see if either of these files are listed as targets and since they aren't it executes the commands given in file1.o's rule and compiles to get the object file.
    4. make looks at the targets file2.o and file3.o and compiles these object files in a similar fashion.
    5. make now has all the object files required to make prog1 and does so by executing the commands in its rule.

    Invoking make

    Make is invoked from a command line with the following format

    make [-f makefile] [-bBdeiknpqrsSt] [macro name=value] [names]

    Frequently used make options
    Command Result
    make use the default descriptor file, build the first target in the file
    make myprog use the default descriptor file, build the target myprog
    make -f mymakefile use the file mymakefile as the descriptor file, build the first target in the file
    make -f mymakefile myprog use the file mymakefile as the descriptor file, build the target myprog