[英]How to use iterators for 2d arrays in c++?
I want to use an iterator for a 2D array but I don't know how.我想对二维数组使用迭代器,但我不知道如何。
My purpose is to access the columns and not the rows.我的目的是访问列而不是行。
I found out that to access the rows I can use this auto it=begin(arr);
我发现要访问行我可以使用这个
auto it=begin(arr);
but I think it isn't the right way to do it, however it seems to work.但我认为这不是正确的方法,但它似乎有效。
#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;
Is this correct?这个对吗?
Thanks in advance:D先谢谢了
If you want to iterate over a column, you can do something like:如果要遍历列,可以执行以下操作:
for(auto it = std::begin(arr); it != std::end(arr); it++)
cout << **it;
This will print the first column.这将打印第一列。
To iterate over the array in column order you would need somenthing like:要按列顺序遍历数组,您需要以下内容:
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;
}
This will print the array in column order.这将按列顺序打印数组。 Note that
std::size
is only available after C++17, you may need to use std=c++17
compiler flag.请注意
std::size
仅在 C++17 之后可用,您可能需要使用std=c++17
编译器标志。
Two side notes:两个旁注:
#include <time.h>
//...
srand(time(0)); //seed
//...
You should not use using namespace std;
你不应该使用
using namespace std;
Why is "using namespace std;" 为什么是“使用命名空间标准;” considered bad practice?
被认为是不好的做法? .
.
Here's a simple pair of iterators that go in row-major and column-major order.这是一对简单的迭代器,go 按行优先和列优先顺序排列。
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); }
You can just do pointer arithmatic to access the column.您可以只做指针算术来访问该列。
cout<<"Column : "<<*(*it+1)<<endl;
Here, 1 is the column number.这里,1 是列号。
Live example here: https://gcc.godbolt.org/z/WXKLCk现场示例: https://gcc.godbolt.org/z/WXKLCk
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.