问题代码:
#include <bits/stdc++.h> #include <vector> using namespace std; int main() { //string 要引用 string s = "hello"; for (auto &i : s) //书上说i 是char类型,那s[n]呢? i = toupper(i); //改变成大写,不影响s的值 // cout << s << endl; //s的值是 hello //array int arr[5] = {1, 2, 3, 4, 5}; for (auto i : arr) //cout << arr[i] << endl; int a[] = {1, 2, 3, 5, 2, 0}; vector<int> counts(a, a + 6); for (auto count : counts) cout << count << " "; cout << endl; }
只跑后面的就能跑:
#include <bits/stdc++.h> #include <vector> using namespace std; int main() { int a[] = {1, 2, 3, 5, 2, 0}; vector<int> counts(a, a + 6); for (auto count : counts) cout << count << " "; cout << endl; }
百思不得其解,最后终于发现是注释的问题:
向for循环加括号就可以跑
去掉注释也可以跑:
#include <bits/stdc++.h> #include <vector> using namespace std; int main() { //string 要引用 string s = "hello"; for (auto &i : s) //书上说i 是char类型,那s[n]呢? i = toupper(i); //改变成大写,不影响s的值 // cout << s << endl; //s的值是 hello //array int arr[5] = {1, 2, 3, 4, 5}; for (auto i : arr) { //cout << arr[i] << endl; } int a[] = {1, 2, 3, 5, 2, 0}; vector<int> counts(a, a + 6); for (auto count : counts) cout << count << " "; cout << endl; }
所以问题是!!!注释
for循环中只有一行代码并且不加大括号并且带打上注释,!!!报错!!!
怀疑是编译器把int a[]这一行也识别成注释了