我正在使用带有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
为什么我得到了不同的结果?
vector<std::string> alist = {"one", "two", "three", "four" };
- MSalters