Never write your own sort! Use the the sort in the Standard Template Library (STL). The STL has sorts that are efficient and well tested.
Basic syntax for calling sort When calling the STL sort, you need to pass two parameters: the address of the first element to sort, and the address of one past the last element to sort. The address is used for iterating across array elements. For other data structures (eg, a vector) you will have to do something a little different, but for arrays we can simply express the beginning and ending points with the array name and the addition of an integer.
[Read More]
Enum in c++
The problem: representing series of values It is very common to have a series of values that need to be represented. For example, to simulate a traffic light requires representing three values (red, yellow, and green), but there is no built-in C++ color datatype.
Use integer values to represent colors, for example red as 0, yellow as 1, and green as 2. There is nothing “green” about the value 2, and it could just as easily be represented by some other number.
[Read More]
Beginning with vi
Unsetting with vi
Eg. to put off ai, use
:set noai
Use the variable closer to its use
Its good programming practice to define the variable closer to its use.
Eg. In cpp, this is better code:
for(int i = 0; i{…}
rather than
int i;
…
…
for(i=0;i
How to bound check arrays in cpp / c
Bound checking in cpp /c is headache….
char \*strcpy(char \*dest, const char \*src) { char \*save \= dest; while(\*dest++ \= \*src++); return save; } //main func
char *src = “hello to c programming language”;
char dest[12];
strcpy(dest,src); //calling function
Here we have no bound check on dest size or src size. When we pass it to function it is perfectly alright but
problem is dest is array which is just 12 bytes long…but src is larger string…
[Read More]
File IO in c++ 1
Introduction
This tutorial will start with the very basis of File I/O (Input/Output) in C++. After that, I will look into aspects that are more advanced, showing you some tricks, and describing useful functions.
You need to have good understanding of C++, otherwise this tutorial will be unfamiliar and not useful to you!
Your Very First Program
I will first write the code, and after that, I will explain it line by line.
[Read More]
Using == operator in better way in cpp
In cpp, it is possible that instead of
i==5
we can do
i=5
So we assign i = 5 and if it is like
if(cond)
cond gets true.
So better is
5==i
beause == is symmetric.
If someone writes by mistake is
5=i
As we get error = ‘can’t assign value to literal’.
Orthogonality
Literally orthogonality means at right angles, hence independent or irrelevant to.
In programming languages, orthogonality means design so that changes in one thing don’t effect another. The example they give a user interface and database — you should be able to swap the database without changing the interface or make changes to the interface without affecting the database.
When this term is used in describing computer instruction sets, orgothogonal instruction set can use any register for any purpose while in non-orthogonal set (such as the Intel Pentium), each register has special properties, e.
[Read More]
Longest common substring revisited
For calculating longest substring we can use following algorithms:
1. Dynamic Algorithm
2. Hirschberg’s algorithm
Troubleshooting DNS servers
There may be broadly 2 problems we face when dealing with DNS server:
The DNS server is not responding to clients. The DNS server does not resolve names correctly. Dealing with them 1 by 1.
The DNS server is not responding to clients
Cause 1: Network failure
Solution: Check if the hardware is fully ok, i.e. adapters are properly plugged or not. Then check network connectivity by pinging other computers or routers (such as its default gateway) that are used and available on the same network as the affected DNS servers.
[Read More]