如何在C++中使用迭代器遍历二维数组?

3

我想在一个二维数组中使用迭代器,但是我不知道该怎么做。

我的目的是访问列而不是行。

我发现要访问行,可以使用这个代码:auto it=begin(arr);,但我认为这不是正确的方法,不过似乎它也能够工作。

#include <iostream>
#include <iterator>

using namespace std;

int main(){
  int arr[3][3];
  for (int i=0;i!=3;i++)
    for (int j=0;j!=3;j++)
      arr[i][j]=rand()%9;

  for (int i=0;i!=3;i++)
    for (int j=0;j!=3;j++){
      cout<<arr[i][j]<<' ';
      if (j==2)
        cout<<'\n';
    }

  auto it=begin(arr);
  cout<<**it<<endl;

这是否正确?

提前感谢:D


你需要迭代器有什么原因吗?2D数组是“基于行”的,所以获取列的迭代器需要编写一个自定义类来处理。 - NathanOliver
@NathanOliver 我需要一个迭代器来访问在每一列找到最大数后的 arr[i+1][it++] 整数,这只是一个示例。 - Seif Boukhatem
3个回答

1

如果你想迭代一个列,你可以这样做:

for(auto it = std::begin(arr); it != std::end(arr); it++)
    cout << **it;

这将打印第一列。
要按列顺序迭代数组,您需要类似以下的内容:
for (size_t i = 0; i < std::size(arr); i++) {
    for (auto it = std::begin(arr); it != std::end(arr); it++) {
        cout << *(*it + i) << " ";
    }
    cout << endl;
}

这将按列顺序打印数组。请注意,在C++17之后才有std::size可用,您可能需要使用std=c++17编译器标志。

实时演示

两个副笔记:

  • 您缺少随机引擎的种子:
  #include <time.h>
  //...
  srand(time(0)); //seed
  //...

这段代码具有未定义的行为。您不能使用指向第一个元素的指针迭代2D数组,因为该指针仅属于第一行,移动到另一行会违反指针加法规则。另外注意您正在访问整个数组,而不是一列。 - NathanOliver
指针加法规则在此处:https://timsong-cpp.github.io/cppwp/expr.add#4.2 - NathanOliver
@NathanOliver,我的理解是OP想要使用由std :: begin给出的迭代器访问数组内部成员,而不是按列顺序打印数组,因此我的答案包含他所需的内容。 - anastaciu
1
@NaturalDemon std::size 只在 C++17 或更高版本中可用。 - anastaciu
1
@anastaciu 我后来发现了,谢谢。 - NaturalDemon
显示剩余4条评论

1
这是一对简单的迭代器,按行主序和列主序进行遍历。
class row_iterator
{
    int (&arr)[3][3];
    std::size_t pos;
public:
    row_iterator(int (&arr)[3][3], std::size_t pos) : arr(arr), pos(pos) {}

    row_iterator& operator++() { ++pos; return *this; }
    row_iterator operator++(int) { auto that = *this; ++pos; return that; }
    int & operator*() { return arr[pos / 3][pos % 3]; }
    int * operator->() { return &arr[pos / 3][pos % 3]; }

    friend bool operator==(row_iterator lhs, row_iterator rhs) { return (lhs.arr == rhs.arr) && (lhs.pos == rhs.pos); }
    friend bool operator!=(row_iterator lhs, row_iterator rhs) { return !lhs == rhs; }
};

row_iterator row_begin(int (&arr)[3][3]) { return row_iterator(arr, 0); }
row_iterator row_end(int (&arr)[3][3]) { return row_iterator(arr, 9); }

class col_iterator
{
    int (&arr)[3][3];
    std::size_t pos;
public:
    col_iterator(int (&arr)[3][3], std::size_t pos) : arr(arr), pos(pos) {}

    col_iterator& operator++() { ++pos; return *this; }
    col_iteratoroperator++(int) { auto that = *this; ++pos; return that; }
    int & operator*() { return arr[pos % 3][pos / 3]; }
    int * operator->() { return &arr[pos % 3][pos / 3]; }

    friend bool operator==(col_iterator lhs, col_iterator rhs) { return (lhs.arr == rhs.arr) && (lhs.pos == rhs.pos); }
    friend bool operator!=(col_iterator lhs, col_iterator rhs) { return !lhs == rhs; }
};

col_iterator col_begin(int (&arr)[3][3]) { return col_iterator(arr, 0); }
col_iterator col_end(int (&arr)[3][3]) { return col_iterator(arr, 9); }

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接