C++ 返回数组的一维?
如何只返回数组的一个维度,而忽略另一个维度 例如:C++ 返回数组的一维?,c++,arrays,2d,C++,Arrays,2d,如何只返回数组的一个维度,而忽略另一个维度 例如: int map[4][8]; int MapManager::getMapX(int x) { return map[x]; } 返回类型应为int*或int[],而不是int: int* MapManager::getMapX(int x) { return map[x]; } 除此之外,您还可以。对于二维数组,您只能通过转换为指针直接返回内部数组的一维数组,例如 int map[4][8]; int* MapMana
int map[4][8];
int MapManager::getMapX(int x)
{
return map[x];
}
返回类型应为
int*
或int[]
,而不是int
:
int* MapManager::getMapX(int x) {
return map[x];
}
除此之外,您还可以。对于二维数组,您只能通过转换为指针直接返回内部数组的一维数组,例如
int map[4][8];
int* MapManager::getMapX(int x)
{
return map[x];
}
对于其他“维度”,您需要另一个外部数组来复制到:
int* MapManager::getMapY(int y, int *outArray, int numElements)
{
for(int i = 0; i < numElements; i++) {
outArray[i] = map[i][y];
}
}
int*MapManager::getMapY(int-y,int*outArray,int-numElements)
{
对于(int i=0;i
此阵列需要分配正确的大小。(在这种情况下为8)
原因是y“列”的数组元素在内存中不是连续的,而是分布在多个数组(即“x”行)中。C数组依赖于这个连续的概念来访问元素。因为这里的其他答案都不返回实际的数组(指针不是数组),所以我想我可以展示如何真正返回数组。或最接近的对象,即对数组的引用
typedef int row_type[8];
row_type& MapManager::getMapX(int x) {
return map[x];
}
这有什么意义?指针的工作原理是一样的
不,没有。指针丢失类型信息,即大小。可以使用数组,但不能使用指针:
// pointer version
int* MapManager::getMapX_ptr(int x) {
return map[x];
}
row_type& row = getMapX(0);
// row is of type int(&)[8]
// notice the size is not lost in the type!
std::sort(begin(row), end(row));
// compiles fine! end() correctly finds the end of the row
int* ptr = getMapX_ptr(0);
// notice how ptr has no size information at all
std::sort(begin(ptr), end(ptr));
// can't possible be made to work!
您不能为int*
写入end
template <typename T, std::size_t N>
T* end(T(&arr)[N]) {
return &arr[0] + N;
}
template <typename T>
T* end(T* ptr) {
// what here?
// guess?
// pick a random number?
}
模板
T*结束(T&arr)[N]){
返回&arr[0]+N;
}
模板
T*end(T*ptr){
//这是什么?
//猜猜看?
//选择一个随机数?
}
您可以创建一个特殊的类,该类提供数组的跨视图。也就是说,它跳过了值。下面是这样的开始:
template<typename T>
class StridedView
{
public:
StridedView(T * b, int stride, int count)
:begin_(b), stride_(stride), count_(count)
{}
template<int N>
StridedView(T (&arr)[N])
:begin_(arr), stride_(1), count_(N)
{}
T & operator[](int index)
{
return *(begin_ + index * stride_);
}
const T & operator[](int index) const
{
return *(begin_ + index * stride_);
}
int size() const { return count_; }
private:
T * begin_;
int stride_;
int count_;
};
模板
类漫游视图
{
公众:
步幅视图(T*b,整数步幅,整数计数)
:开始(b)、步幅(步幅)、计数(计数)
{}
模板
步态视图(T&arr)[N])
:开始(arr)、步幅(1)、计数(N)
{}
T&运算符[](整数索引)
{
返回*(开始+索引*步幅);
}
常量T&运算符[](整数索引)常量
{
返回*(开始+索引*步幅);
}
int size()常量{返回计数}
私人:
T*开始;
int-stride_2;;
整数计数;
};
然后,您可以使用适当的函数获取行或列:
template<typename T, int R, int C>
StridedView<T> GetRow(T (&arr)[R][C], int row)
{
T * begin = (*arr) + (row * C);
return StridedView<T>(begin, 1, C);
}
template<typename T, int R, int C>
StridedView<T> GetColumn(T (&arr)[R][C], int column)
{
T * begin = (*arr) + column;
return StridedView<T>(begin, C, R);
}
模板
大踏步视图GetRow(T(&arr)[R][C],int row)
{
T*begin=(*arr)+(第*C行);
返回SteadView(开始,1,C);
}
模板
SteadView GetColumn(T(&arr)[R][C],int列)
{
T*begin=(*arr)+列;
返回SteadView(开始,C,R);
}
您是否在询问如何实现
getRow
和getCol
?:)