vi命令

(转载)
进入vi的命令
Vi   filename    打开或新建文件,并将光标置于第一行首
Vi   +n   filename 打开文件,并将光标置于第n行首
Vi   +    filename  打开文件并将光标置于最后一行首
Vi   +/pattern filename 打开文件,并将光标置于一个与pattern匹配的串处
Vi   filename .. Filename  打开多个文件,依次进行编辑

What is the "Named Constructor Idiom"?

A technique that provides more intuitive and/or safer construction operations for users of your class.

The problem is that constructors always have the same name as the class. Therefore the only way to differentiate between the various constructors of a class is by the parameter list. But if there are lots of constructors, the differences between them become somewhat subtle and error prone.

With the Named Constructor Idiom, you declare all the class's constructors in the private or protected sections, and you provide public static methods that return an object. These static methods are the so-called "Named Constructors." In general there is one such static method for each different way to construct an object.

Problems with inline functions in C++

(From http://)

Besides the problems associated with inline expansion in general, inline functions as a language feature may not be as valuable as they appear, for a number of reasons:

    * Often, a compiler is in a better position than a human to decide whether a particular function should be inlined. Sometimes the compiler may not be able to inline as many functions as the programmer indicates.
    * An important point to note is that the code (of the inline function) gets exposed to its client(the calling function).

Bubble Sort

The Bubble sort has a worst-case running time of Θ(n^2).

//compare the consecutive numbers, larger one sinks to the bottom
void bubbleSort(int *a)
{
// int temp;
  for(int j = 0; j < arraysize; j++) 
  { 
    for( int i = 0; i < arraysize - j - 1;i++ )
    { 
       if( a[i] > a[i+1] ) 
      {swap(a[i], a[i+1]);} 
      }
  }
}

Heapsort

“Like merge sort, but unlike insertion sort, heapsort’s running time is O(n*lg(n)). Like insertion sort, but unlike merge sort, heapsort sorts in place: only a constant number of array elements are stored outside the input array at any time. Thus, heapsort combines the better attribute of the two sorting algorithms …” [Introduction to Algorithms]

void heapSort(int *a)
{
    for(int i = arraySize; i > 1; --i)
    {
        for(int k = 0; k < i; ++k)
        {
            // compare each element with its parent
            // if the child is begger than its parent
            // then swap them
            // keep doing this until reaching the root
            while((a[k] > a[k/2]) && (k > 0))
            { 
                swap(a[k], a[k/2]); 
                k = k/2; 
            }
        }
        // After the for loop, the largest is put at a[0]
        // then put the largest to the end of the array
        // and find the largest of the rest in the same way
        swap(a[0], a[i-1]);
    }
}

Merge Sort

The merge sort has a worst case running time of Θnlg(n).

Insertion Sort

Insertion sort has a worst-case running time of Θ(n^2).
void insertionSort(int *a, int n)
{
    for (int j = 1; j < n; ++j)
    {
        int key = a[j];

        // Insert A[j] into the sorted sequence A[0, 1,...,j-1]
        int i = j-1;
        while ((i >= 0) && (key < a[i]))
        {
            a[i+1] = a[i];
            --i;
        }
        a[i+1] = key;
    }
}