Re: [問題] 字串與字元陣列的比較
※ 引述《xcycl (XOO)》之銘言:
: ※ 引述《asail0712 (emeth)》之銘言:
: : std::string a = "ABC";
: : char b[4] = "ABC";
: : if(!strcmp(a.c_str(), b))
: : {
: : ....
: : ....
: : ....
: : }
: : if(a == std::string(b))
: std::string 有 overload 等式判斷,包括左邊或右邊是 char*,所以只要
: a == b 就可以了 ..
: 推 asail0712:我舉例沒有在loop裡面 實際上是有啊~!! 124.11.134.41 07/10 02:33
: → asail0712:即使是overload 事實上 也事先把 char* 124.11.134.41 07/10 02:33
: → asail0712:轉成std::string 然後在比較對吧 124.11.134.41 07/10 02:34
這就不一定了
因為這個 overloaded operator == 是屬於std::string的
也就是說 它可以去挖同屬於 std::string 的物件的內容來做比較
這樣一來就不會有為了比較重新建構物件的 overhead 了
當然實際實作上仍然要看各家自己的做法
--
"LPH" is for "Let Program Heal us"....
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.254.23.234
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):