星期二, 11月 22, 2011

利用sregex_iterator找出所有符合指定regular expression的子字串

<<程式>>


#include <regex>
#include <string>
#include <iostream>
using namespace std;
 
int _tmain(int argc, _TCHAR* argv[])
{
 try{
  regex re("\\d++");
  string input("123 23 435 asd 000");
  sregex_iterator ite(input.begin(), input.end(), re);
  sregex_iterator iteEnd;
  for(; ite != iteEnd; ++ite){
   cout << "found: " << ite->str() << " at " << ite->position() << " length " << ite->length() << endl;
  }
 } catch(regex_error& e){
  cout << "Error: " << e.what() << endl;
 }
 return 0;
}


<<輸出>>


found: 123 at 0 length 3
found: 23 at 4 length 2
found: 435 at 7 length 3
found: 000 at 15 length 3

星期一, 5月 09, 2011

在c#中正確處理Unicode

程式碼:

string s = "哈\uD950\uDF21高";   Console.WriteLine("string length:" + s.Length);  
TextElementEnumerator textEnum =
   StringInfo.GetTextElementEnumerator(s);   while (textEnum.MoveNext())   {   
   Console.WriteLine("word {0}: {1}",
     textEnum.ElementIndex, 
     textEnum.Current.ToString());  
}
* This source code was highlighted with Source Code Highlighter.

輸出:
string length:4
word 0: 哈
word 1: ??
word 3: 高

重點:

  • UTF-32等同於UCS-4 (Universal Character Set –4 ), 編碼範圍在0~7F FF FF FF
  • UTF-16不等同於UCS-2。USC-2只涵蓋部分USC-4的字元。而UTF-16可以涵蓋所有UTF-32的字元
  • UTF-16利用surrogate機制,讓某些UTF-32的字元以一對UTF-16編碼的形式來表示
  • USC-2跟UTF-16的差別在於有無surrogate機制上
  • .Net使用的char型別存放的是UTF-16

星期二, 1月 25, 2011

Doxgen筆記

格式:

/**
  * @warning A Warning!!
  */

星期一, 1月 10, 2011

在Microsoft Access筆記中

參照到使用者介面物件

  • [Forms]![Questions]![RawData 子表單].Form![answer]

取得目前使用者

星期日, 10月 24, 2010

Visual Studio的編譯會受Windows SDK版本設定的影響

幾個簡單檢視目前Windows SDK版本的方法:

  • 方法一: 開啟Visual Studio Command Prompt, 下echo %windowssdkdir%指令
  • 方法二: 開啟vc專案,找到”Include Directories”之類的設定項目,在編輯畫面的Macros中檢視 $(WindowsSDKDir)

Windows SDK版本設定如果不正確,可能會導致vc專案編譯失敗,出現類似像windows.h找不到的錯誤訊息。

修正方式:

  • 在windows SDK目錄下,使用以下命令來設定(以下命令將SDK版本設定為v7.0A):
    C:\Program Files\Microsoft SDKs\Windows\v7.1\Setup>WindowsSdkVer.exe -version:v7.0A

參考資料

星期二, 8月 10, 2010

嘗試在VS2010中使用C++0x的shared_ptr以及lambda expression搭配STL container

#include <iostream>
#include <vector>
#include <memory>
#include <algorithm>

using namespace std;

class Base{
public:
    int m_i;
    Base(int i):m_i(i) {}
    ~Base(){ cout << m_i << "~Base()" << endl; }
};

class Derived: public Base{
public:
    Derived(int i):Base(i) { }
    ~Derived(){
        cout << "~Derived()" << endl;
    }
};

int main()
{
    vector< shared_ptr<Base> > v;
    v.push_back(shared_ptr<Base>(new Derived(1)));
    v.push_back(shared_ptr<Base>(new Derived(2)));
    sort(v.begin(), v.end(), [](shared_ptr<Base> a, shared_ptr<Base> b){
        return a->m_i > b->m_i;
    });

    for_each(v.begin(), v.end(), [](shared_ptr<Base> a){
        cout << a->m_i << "\t";
    });
    cout << endl;
    return 0;
}

星期四, 7月 08, 2010

莫名其妙的簡繁轉換

葉:叶
舊:旧
盡:尽