c++ - forloop、迭代器和 vector 不配合我

标签 c++ vector iterator

为什么这对我来说很好:

 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()

关于c++ - forloop、迭代器和 vector 不配合我,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11939368/

相关文章:

php - 将 Crypto++ AES 加密移植到 PHP 的 mcrypt 时 key 大小不正确

r - 将向量如 c ("x","y","z") 转换为 function(x , y, z){}

ios - 在 Xcode 7 中将 PDF 用于 Assets - SpriteKit

Android Studio 矢量资源失真

c++ - 数组中字符串的迭代器

c++ - 如何使用作为参数传递的信息来构建在其他 CPP 文件中定义的字符串

c++ - 数组长度,为什么从命令行中获取时不能使用它?

for-loop - Rust 中 for 循环的确切定义是什么?

c++ - gdb,多个位置的断点

c++ - 嵌套 vector 迭代器 - 仅显示第 n 个 vector