Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
为什么这对我有用:
for(int i = 0; i < vec.size(); i++) { os << vec[i] << " "; }
虽然这不是:
for(vector<int>::iterator it = vec.begin(); it < vec.end(); it++) { os << vec[*it] << " "; }
您应该打印*it而不是将其用作索引,并且您可能应该将条件更改为it != vec.end().
*it
it != vec.end()
您使用错误的迭代器,它应该是:
for(vector<int>::iterator it = vec.begin(); it < vec.end(); it++) { os << *it << " "; }
您的代码只是尝试打印 index 处的元素*it,这甚至可能无效。