-1
我讀了做這樣的事情如果創建本地副本,使用參數「const [variable_type]&」的目的是什麼?
void print_in_reverse (const std::string & str)
{
std::string::const_iterator beg(str.cbegin()), it(str.cend());
if (it != beg)
while (--it != beg) std::cout << *it;
}
將創建傳入的字符串的一個本地副本。那麼,什麼是上面的地步?它不是在計算上比外行人的版本更高效,
void print_in_reverse (std::string str)
{
std::string::const_iterator beg(str.cbegin()), it(str.cend());
if (it != beg)
while (--it != beg) std::cout << *it;
}
我知道通過引用是避免本地副本的方式,但我記得在C++入門讀物中傳遞一個const引用將會創建一個本地副本 – 2014-10-30 15:37:22
您是否有確切的引用? – Borgleader 2014-10-30 15:41:42
@DonaldKnuthLied:據推測,它說傳遞一個不同的類型會導致創建一個臨時(在調用函數中 - 不是被調用函數的本地),正如我所描述的那樣。 – 2014-10-30 15:42:17