繁体   English   中英

如何使用vector.begin()和vector.end()遍历矩阵?

[英]How would I iterate through a matrix using vector.begin() and vector.end()?

我知道这不切实际,并且我总是可以使用.size()或auto&col:matrix,但是如何使用以下方法遍历矩阵:

   for (std::vector<vector<int>>::iterator col = matrix.begin(); col != matrix.end(); ++col){
       for (std::vector<int>::iterator row = matrix[col].begin(); row != matrix[col].end(); row++){
          std::cout << matrix[col][row] << std::endl;
   }
}

我认为这是一个糟糕的主意,但想找到一种方法来处理2D向量。 但是,如果您能弄清楚,那就太好了。

您可以这样操作:

std::vector<std::vector<int>> matrix;

for (auto col = matrix.begin(); col != matrix.end(); ++col)
    for (auto el = col->begin(); el != col->end(); ++el)
        std::cout << *el << std::endl;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM