作者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