[問題] struct 當中的 reference type
想請教一些問題:
1. 下面程式碼當中的strcut CMP,是POD type嗎?
2. 建立(CMP){s}的時候,會複製一份字串嗎?還是只傳遞reference呢?
3. (CMP){s}是在compile-time建立?還是在run-time建立呢?
謝謝
// suffix array
#include <string>
#include <vector>
#include <algorithm>
struct CMP {
std::string& s;
bool operator()(const int& i, const int& j) {
return s.substr(i) < s.substr(j);
}
};
int main() {
std::string s = "hello world!";
std::vector<int> array(s.size());
for (int i=0; i<(int)s.size(); ++i) array[i] = i;
std::sort(array.begin(), array.end(), (CMP){s});
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.225.132.97
→
04/04 21:17, , 1F
04/04 21:17, 1F
→
04/04 22:41, , 2F
04/04 22:41, 2F
→
04/04 23:37, , 3F
04/04 23:37, 3F
推
04/04 23:46, , 4F
04/04 23:46, 4F
→
04/05 08:31, , 5F
04/05 08:31, 5F
→
04/05 08:34, , 6F
04/05 08:34, 6F
→
04/05 08:53, , 7F
04/05 08:53, 7F
→
04/05 08:54, , 8F
04/05 08:54, 8F
→
04/05 09:06, , 9F
04/05 09:06, 9F
→
04/05 09:08, , 10F
04/05 09:08, 10F
推
04/05 12:09, , 11F
04/05 12:09, 11F
→
04/05 12:43, , 12F
04/05 12:43, 12F
→
04/05 12:44, , 13F
04/05 12:44, 13F
→
04/05 12:45, , 14F
04/05 12:45, 14F
→
04/05 12:45, , 15F
04/05 12:45, 15F
→
04/05 16:20, , 16F
04/05 16:20, 16F
→
04/05 19:38, , 17F
04/05 19:38, 17F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章