作者ric2k1 (Ric)
看板EE_DSnP
標題[建議] string and vector
時間Wed Oct 12 21:35:09 2011
同學們可能還不太熟悉 string 與 vector 的使用方式,
建議大家可以看一下 SGI 的 STL guide,
http://www.sgi.com/tech/stl/table_of_contents.html
至少先大概看一下有哪些 member functoins 可以使用,
一些 must-know 像是:
* 把一部分的 sub-string 抽出來:
string ss = s.substr(i, j-i-1); // copy string s[i, j) to ss
string ss = s.substr(i, 4); // copy string s[i, i+4] to ss
* 找字串裏頭的某一個字元:
int idx = s.find(c, i); // 從第 i 個字元開始找 c, return c 所在之 index
* 把字串裏頭的 const char * 抓出來:
const char* cc = ss.c_str();
* 把字串加到 vector 後面:
_history.push_back(ss); // _history.size() will be increaszed by 1
* 把字串放到 vector 的最後一個有效位置:
_history.back() = ss; // _history.size() will NOT change
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.248.108.54
1F:→ wmin0:find_first/last_of 系列也很好用 10/12 22:04
2F:推 gamerred:substr第二個參數是子字串長度喔...應該是[i,i+j) 10/12 23:47
※ 編輯: ric2k1 來自: 111.248.108.54 (10/13 00:11)
3F:→ ric2k1:哦 哦! 寫錯,感謝糾正!! 10/13 00:11