2010年2月26日 星期五

[C++][STL] delete item in loop

STL 好用是好用,小地方還是要注意,不然很容易爆炸又難DEBUG

1. Use List:
list中erase會回傳iterator,所以要erase(it)
list<int> tmp;
for(list::iterator it = tmp.begin() ; it != tmp.end() ; /*empty*/ )
{
        if( something )
                it = tmp.erase(it);
        else
                it++;
}

2. Use Map:
map中erase竟然又不一樣,回傳void,所以需要erase(it++)
map<int,int> tmp;
for(map::iterator it = tmp.begin() ; it != tmp.end() ; /*empty*/ )
{
        if( something )
                tmp.erase(it++);
        else
                it++;
}

其實應該要看他內部為啥實做成兩種模式的
但是STL聖經一直供在桌上懶得看XD
有空再來研究

[Blog] 在blogger貼漂亮的程式碼

換身為一個宅宅工程師,在BLOG上貼漂亮的程式碼也是合情合理的(推眼鏡)

上網可以搜尋到一缸子的blogger解法
很囧的是不知道為啥很多人貼的解法我都弄不起來
經過多次嘗試失敗
一怒之下直接去看人家的原始網頁
然後就成功貼出程式碼了XD

我是範例程式碼
int main()
{
   printf("hello world");
   return 0;
}
我是範例程式碼

主要步驟記錄一下:
1. Blogger後台選擇〔版面配置〕→〔修改HTML〕→勾選〔展開小裝置範本〕
2. 把以下這段CSS貼到裡面,貼哪裡不知道可以看網頁原始碼
.post pre {
background: #F0F0F0 url(http://klcintw.images.googlepages.com/Code_BG.gif) repeat-y scroll left top;
border: 1px solid #CCCCCC;
color: #333333;
display: block;
font-family: Courier New;
font-size: 10pt;
line-height: 120%;
margin: 5px 0 0 20px;
max-height: 200px;
overflow: auto;
padding: 10px 10px 10px 21px;
width: 90%;
}
.CODE  {
display: block; /* fixes a strange ie margin bug */
font-family: Courier New;
font-size: 8pt;
overflow:auto;
background: #f0f0f0 url(http://klcintw.images.googlepages.com/Code_BG.gif) l\
eft top repeat-y;
border: 1px solid #ccc;
padding: 10px 10px 10px 21px;
max-height:200px;
height:200px; // for IE6
line-height: 1.2em;
}

3. 麻煩的大於小於轉換:
HTML ENCODE網站去轉換

4. 在<pre class="code">和</pre>之間插入你的程式碼

不過blogger貼程式碼還是不太friendly阿~_~||

參考文章:
高登工作室
Forgot 同花打不打得過俘虜浩司
SeekING Bernie

2010年2月3日 星期三

[C++][STL] map如何使用一個struct當作key

拿來當key的struct,裡面有兩個variables:
struct S
{
        int x;
        int y;
};
由於map在insert時就會用KEY做排序,所以必須自己寫一個比較函式:
struct CmpFunction
{
        bool operator() ( const struct S s1, const struct S s2 ) const
        {
                return ( (s1.x < s2.x) || ((s1.x == s2.x) && (s1.y < s2.y)) );
        }
};
接下來就可以直接定義map的typedef,比較簡單好用:
typedef std::map<struct S, int value , CmpFunction> map_t;
宣告:
map_t mymap;
如此一來就可以用find去快速搜尋:
struct S mystruct;
map_t::iterator iter = mymap.find( mystruct );
if( iter == mymap.end() ) // not found
else // found

2010年2月1日 星期一

那年,我們約館麥


最近在捷運上或公車上常聽到學生模樣的人聊天
「ㄟ 等下約北車喔!」 「好,北車見」
北車北車,短暫的疑惑直到他們陸續在台北車站下車我才啞然失笑
原來北車就是台北車站的簡稱阿,這群小鬼真是...
「台北車站就台北車站,什麼北車南車」
我一邊踏出車廂一邊暗自搖頭
走出擁擠的人潮,卻不經意地抬頭看見赫哲旁邊鮮黃色的大M招牌
原來,八、九年前的我們,

一樣的嘻嘻哈哈跨出車廂,
一樣的打著手機呼朋引伴,
可能也一樣的有一位老頭在旁邊嗤之以鼻:
「館前路麥當勞就館前路麥當勞,什麼館賣館買」


那年,我們約館麥


--獻給所有曾經約在館麥見面的高中好友們