我正在 Mac OX (LLVM 4.2) 附带的 Clang 编译器上试验 C++11 功能,以下结果让我感到困惑:
// clang compile with "c++ -std=c++11 -stdlib=libc++"
#include <iostream>
#include <vector>
int main(void) {
using namespace std;
vector<int> alist={1, 2, 3, 4};
for (int i=0; i<alist.size(); i++) {
cout << alist[i] << " ";
}
cout << endl;
for (auto i: alist) {
cout << alist[i] << " ";
}
cout << endl;
return 0;
}
在运行环境中,我得到不同的输出,如下所示:
1 2 3 4
2 3 4 0
为什么我会得到不同的结果?