效率
1. 避免拷贝,提高效率
// 低效 - 会拷贝整个对象 void processString(string str); // 高效 - 只传递引用 void processString(const string& str); // 对于大型对象(如vector、自定义类),引用避免拷贝可以显著提升性能 void processVector(const vector<int>& vec);
2. 防止修改原始数据
void display(const string& str) { // str不能被修改,保护原始数据 // str[0] = 'A'; // 错误!编译时报错 cout << str; }

浙公网安备 33010602011771号