Wednesday, February 20, 2013

Basic Things About namespace in C++

這回是演算法課,老師提供一個Judge網頁(https://better.cs.nctu.edu.tw/2013_spring_algo_hw1/)給大家練習題目,可是它系統與之前用的解題系統略有不同,其中我們需要完成的是一份cpp檔案裡的一個namespace內的東西。所以藉這機會再瞭解一下namespace。

namespace顧名思義是指命名空間,底下可以有變數、函示的定義,使用時候可以在程式碼裡面加using namespace <name_of_the_namespace>即可,若是不加,則在要使用該namespace下的東西前面加來自的地方,例如:寫using namespace std; 之後,可以直接用cout;而若是沒寫,則可以用std::cout。

老師提供的空白檔內有一個稱為HOMEWORK的namespace,底下有三個函式,分別是NumberOfInversion(), Init(), Cleanup(),Init和Cleanup即是初始和結束要做的事情,NumberOfInversion是依照題目要求要完成的部分。

然而完成這份namespace之後要怎麼測試呢?可以寫個main.cpp如下(code.empty.cpp是定義namespace的檔案,也是題目要求我們要完成的):

#include <iostream> 
#include "code_empty.cpp"

using namespace std;

int main(){ 

    using namespace HOMEWORK;

    Init(); 

    int arr[] = {0,1,4,3,2}; 
    cout << NumberOfInversion(arr, 5) << endl;;

    Cleanup(); 

    return 0; 
}

*記得在編譯main.cpp時候要注意include code_empty.cpp的情形(視編譯環境)


Friday, February 1, 2013

Controlling Virtualbox From Command Line & SSH From Host to Client

Here, I got some virtual machines on my laptop. And I would like to learn how to control them via command line. The controls I am interested in includes, "start", "power off", "save current state", etc.

Take the "Fedora" virtual machine as example.

  • VBoxManage startvm "Fedora"
    • Turn on the virtual machine
  • VBoxManage startvm "Fedora" --type headless
    • Start the virtual machine without really start the window
  • VBoxManage controlvm "Fedora" savestate
    • Save the current state of the virtual machine and turn off the window
Setting up connection from host to client using ssh is also easy.
  1. on the setting page of the virtual machine
  2. click on network tab, and add a new adapter (usually, it's adapter 2)
  3. attached to "host-only adapter", with name "vboxnet0"
    • if virtualbox shows "invalid setting" in the lower part, go to the "preference" page of virtualbox, then find "network" tab, and add a new setting by pressing the button on the right hand side
  4. start the virtual machine, and click IP by typing "ifconfig"
  5. enable ssh server on the virtual machine, you may check it work or not by trying ssh to itself
  6. ssh to the IP you saw on the client with the right username