std::remove_extent返回数组降低一个维度后的数据类型, std::rank返回数组维度, std::extent返回数组第一个维度的长度

本文介绍了C++标准库中的std::remove_extent、std::rank和std::extent三个模板,它们分别用于处理数组类型。std::remove_extent返回去掉一个维度后的数组元素类型,std::rank给出数组的总维度,而std::extent则返回数组的第一个维度的长度。这些工具对于理解和操作多维数组非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

std::remove_extent
 返回数组降低一个维度后的数据类型。不改变数据类型的限制属性(const, volatile, const volatile)
 一维数组降低到0维度;
 二维数组降低到一维数组;
 三维数组降低到二维数组;

std::extent
 返回数组第一个维度的长度

std::remove_all_extents
 返回数组的实际类型, int[] -> int, int[][2] -> int, int[][3][4] -> int

    int arrO[] = { 1,2,3,4 };
	int arrT[2][3] = { {1,2,3},{3,4,5} };
	std::remove_extent<decltype(arrO)>::type oa{}; // int
	std::remove_extent<decltype(arrT)>::type ob{}; // int[3]
	cout << std::extent<decltype(arrO)>::value << endl; // 4,  一维数组的长度
	cout << std::extent<decltype(arrT)>::value << endl; // 2 , 二维数组的第一维度的长度

	const int arrF[3] = {};
	const int arrS[4][3] = { {1,2,3},{4,5,6} };
	std::remove_extent<decltype(arrF)>::type oc{}; // const int
	std::remove_extent<decltype(arrS)>::type od{}; // const int[3]
	cout << std::extent<decltype(arrF)>::value << endl; // 3
	cout << std::extent<decltype(arrS)>::value << endl; // 4

	volatile int arrD[] = { 1,2 };
	volatile int arrE[][2] = { {1,2},{3,4}, { 5,6 } };
	std::remove_extent<decltype(arrD)>::type oe{}; // volatile int
	std::remove_extent<decltype(arrE)>::type of{}; // volatile int[2]
	cout << std::extent<decltype(arrD)>::value << endl; // 2
	cout << std::extent<decltype(arrE)>::value << endl; // 3

	const volatile int arrQ[] = { 1,2 };
	const volatile int arrP[][3] = { {1,2,3},{4,5,6} };
	std::remove_extent<decltype(arrQ)>::type og{}; // const volatile int
	std::remove_extent<decltype(arrP)>::type oh{}; // const volatile int[3]
	cout << std::extent<decltype(arrQ)>::value << endl; // 2
	cout << std::extent<decltype(arrP)>::value << endl; // 2

std::rank

返回数组的维度

int arr[] = { 1,2 };
int arrA[][3] = { {1,2,3} };
int arrB[1][2][3] = {};
std::rank<decltype(arr)>::value << endl;  // 1, 一维数组
std::rank<decltype(arrA)>::value << endl; // 2 , 二维数组
std::rank<decltype(arrB)>::value << endl; // 3 , 三维数组
template<class _Ty, _Ty _Val>
struct integral_constant
{
	static constexpr _Ty value = _Val;

	using value_type = _Ty;
	using type = integral_constant;
    
    // intergral_constant可以像int一样使用
	constexpr operator value_type() const noexcept
	{
		return (value);
	}
    
    // intergral_constant可以像int一样使用
	constexpr value_type operator()() const noexcept
	{
		return (value);
	}
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值