2011-02-03 5 views
0

可能性の重複:
Is it better in C++ to pass by value or pass by constant reference?なぜ参照渡しが良いですか?

これら2プログラムを参照してください。

bool isShorter(const string s1, const string s2); 

int main() 
{ 
    string s1 = "abc"; 
    string s2 = "abcd"; 
    cout << isShorter(s1,s2); 
} 

bool isShorter(const string s1, const string s2) 
{ 
    return s1.size() < s2.size(); 
} 

bool isShorter(const string &s1, const string &s2); 

int main() 
{ 
    string s1 = "abc"; 
    string s2 = "abcd"; 
    cout << isShorter(s1,s2); 
} 

bool isShorter(const string &s1, const string &s2) 
{ 
    return s1.size() < s2.size(); 
} 

もう一つは優れているのはなぜ?

答えて

0

文字列をコピーする必要がないためです。

0

を読むことをお勧めthis.

関連する問題