C++ 如何在C+中的多维数组中选择范围+;?
假设我有:C++ 如何在C+中的多维数组中选择范围+;?,c++,arrays,for-loop,matrix,multidimensional-array,C++,Arrays,For Loop,Matrix,Multidimensional Array,假设我有: int array[9][9]= { {1 , 2 , 3 , 4, 5, 6, 7, 8, 9}, {10, 11, 12, 13, 14, 15, 16, 17, 18}, {19, 20, 21, 22, 23, 24, 25, 26, 27}, {28, 29, 30, 31, 32, 33, 34, 35, 36}, {37, 38, 39, 40, 41, 42, 43, 44, 45},
int array[9][9]= {
{1 , 2 , 3 , 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27},
{28, 29, 30, 31, 32, 33, 34, 35, 36},
{37, 38, 39, 40, 41, 42, 43, 44, 45},
{46, 47, 48, 49, 50, 51, 52, 53, 54},
{55, 56, 57, 58, 59, 60, 61, 62, 63},
{64, 65, 66, 67, 68, 69, 70, 71, 72},
{73, 74, 75, 76, 77, 78, 79, 80, 81}
};
如何仅对第一行(值1到9)或第一列(如值1到73)应用某些函数。假设我想说索引0到9都应该有值0
是否可以将此范围保存在变量中 要隔离数组的行,可以引用数组的行:
int (&row)[9] = array[2];
void boo(int * col, int rows, int cols) {
for (int row = 0; row < rows; ++row) {
int * x = col + row * cols;
}
}
....
// process fourth column:
boo(array[0]+4, 9, 9);
例如,上面的行引用数组的第三行
对于柱而言,更为复杂
或者,您可以执行以下构造,将引用包装的向量返回到二维数组的列或行
// if flg == true you get row at idx else if flg == false you get column at idx
template<typename T, int N, int M>
std::vector<std::reference_wrapper<T>>
getRange(T (&arr)[N][M], std::size_t const idx, bool const flg = true) {
if(flg) {
return typename std::vector<std::reference_wrapper<T>>(std::begin(arr[idx]), std::end(arr[idx]));
} else {
typename std::vector<std::reference_wrapper<T>> out;
out.reserve(N);
for(int i(0); i < N; ++i) out.push_back(arr[i][idx]);
return out;
}
}
//如果flg==true,则在idx处获取行;如果flg==false,则在idx处获取列
模板
向量
getRange(T(&arr)[N][M],std::size\u T const idx,bool const flg=true){
如果(flg){
返回typename std::vector(std::begin(arr[idx]),std::end(arr[idx]);
}否则{
typename std::vector out;
out.保留(N);
for(inti(0);i
试着这样做:
for (int i = 0; i<10; i++)
array[0][i] = 0;
对于(int i=0;i对于行,这很容易,因为您可以像这样传递它们:
void foo(int * row, int cols) {
for (int col = 0; col < cols; ++col) {
int * x = row + col;
}
}
...
foo(array[3], 9);
...
void foo(int*row,int cols){
for(int col=0;col
对于列,这更为困难,但您可以将每一列都视为数组中具有特定偏移量的列:
int (&row)[9] = array[2];
void boo(int * col, int rows, int cols) {
for (int row = 0; row < rows; ++row) {
int * x = col + row * cols;
}
}
....
// process fourth column:
boo(array[0]+4, 9, 9);
void boo(int*col、int行、int列){
对于(int行=0;行<行;++行){
int*x=列+行*列;
}
}
....
//处理第四列:
boo(数组[0]+4,9,9);
当然,使用sieOf代替“9”和C++向量/数组代替C样式int [][]会使生命更加容易,代码更易于阅读和支持。
另一种方法是使用boost::matrix,例如:
using namespace boost::numeric::ublas;
matrix<double> m(9, 9);
matrix_row<matrix <double> > row(m, 5);
matrix_column<matrix <double> > col(m, 4);
使用名称空间boost::numeric::ublas;
矩阵m(9,9);
矩阵_行(m,5);
矩阵_列col(m,4);
您可以通过使用函数指定索引(开始和结束范围)来完成此操作,并说明是否应将其应用于行或列。由于您使用的是纯C样式数组,因此处理指针更为棘手。我建议您使用向量
s和对
s(用于范围)
C样式数组的一个示例
void some_function(int array[][9], bool row_or_column, size_t major, size_t start, size_t end){
if (row_or_column = true) {
for (int i = start; i < end; i++) {
cout << array[major][i]; //perform your operation on row
}
}
else {
for (int i = start; i < end; i++) {
cout << array[i][major]; //perform your operation on column
}
}
}
void some_函数(int array[][9],bool row_或_column,size_t major,size_t start,size_t end){
if(行或列=真){
for(int i=start;i cout在C中没有真正的多维数组
在一个真正的多维数组中,所有的维度都是平等的。不管你能对行做什么,你也能对列做什么
<>这不是C++的情况。你的数组的第三行只是< /p>
array[3]
它在各个方面都是一个数组。一系列行与任何其他范围一样,可以表示为(开始、结束)对,例如make\u对(数组[3],数组[7])
第三列与第三行不同,它不是一个数组,它只是一个虚拟的元素集合,不在任何标准数据结构的保护伞下
一个多维数组切片最接近的是自定义迭代器,使得>++i移到右边的下一个元素或下一个元素。当你在它的时候,考虑从C样式数组移到STL样式容器。< /P>可能的副本查看一下“代码> STD::切片它就是这样做的。您不能转换int[9][9]to int**非常简单,解决了我的问题。谢谢。现在再问一个问题。现在我想测试这个范围,看一个数字是否重复。例如,我想确保那一行或那一列中只有一个5。你会怎么做?看这篇文章,类似的问题非常感谢。我想确保只有一个oNE5在那一行或那一列。你会怎么做?