效率

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;
}

 

posted @ 2026-02-05 12:59  家煜宝宝  阅读(3)  评论(0)    收藏  举报