如何确定数组元素的类型?

时间:2021-10-22 21:40:38

I can't get the type of an element. This solution returns a reference to element type.

我无法获得元素的类型。此解决方案返回对元素类型的引用。

int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = decltype(*arr);
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));

2 个解决方案

#1


10  

Try the following

请尝试以下方法

using arrElemType = std::remove_reference<decltype( *arr )>::type;

or

typedef std::remove_reference<decltype( *arr )>::type arrElemType;

You need to include header <type_traits>

您需要包含标题

#2


1  

The standard way in C++11 and above is to use std::remove_all_extents.

C ++ 11及更高版本中的标准方法是使用std :: remove_all_extents。

#include <type_traits>

int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = std::remove_all_extents<decltype(arr)>::type;
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));

#1


10  

Try the following

请尝试以下方法

using arrElemType = std::remove_reference<decltype( *arr )>::type;

or

typedef std::remove_reference<decltype( *arr )>::type arrElemType;

You need to include header <type_traits>

您需要包含标题

#2


1  

The standard way in C++11 and above is to use std::remove_all_extents.

C ++ 11及更高版本中的标准方法是使用std :: remove_all_extents。

#include <type_traits>

int arr[] = { 0, 1, 2, 3, 4, 5 };
using arrElemType = std::remove_all_extents<decltype(arr)>::type;
vector<arrElemType> vec(std::cbegin(arr), std::cend(arr));