與以下代碼的for循環一樣,爲什麼我們必須使用引用(&c
)更改c
的值。爲什麼我們不能在for
循環中使用c
。 也許它是關於參數和參數之間的區別?for循環中的C++引用
#include "stdafx.h"
#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
int main()
{
string s1("Hello");
for (auto &c : s1)
c = toupper(c);
cout << s1 << endl;
return 0;
}
Thx很多爲您的答案。真的幫助我理解。 – LiuHao