Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 获取std::vector/std::array的维数_C++_C++11_Templates_C++14 - Fatal编程技术网

C++ 获取std::vector/std::array的维数

C++ 获取std::vector/std::array的维数,c++,c++11,templates,c++14,C++,C++11,Templates,C++14,假设我想要一个class/struct类型,继承自integral\u constant,其中N是维度,维度实现如下: template<class T> struct dimension; template<class T> struct dimension<vector<T>> : integral_constant<size_t, 1> {}; template<class T> struct dimension&

假设我想要一个
class/struct
类型,继承自
integral\u constant
,其中N是维度,维度实现如下:

template<class T>
struct dimension;

template<class T>
struct dimension<vector<T>> : integral_constant<size_t, 1> {};

template<class T>
struct dimension<vector<vector<T>>> : integral_constant<size_t, 2> {};
模板
结构维度;
模板
结构维数:积分常数{};
模板
结构维数:积分常数{};
然后

cout << dimension<vector<int>>::value;         // 1
cout << dimension<vector<vector<int>>>::value; // 2

cout您可以对
std::vector
执行此操作(注意
std::vector
的模板参数列表大于1):

下面是一个简单的工作示例:

#include<vector>
#include<iostream>

template<typename T>
struct dimension { static constexpr std::size_t value = 0; };

template<typename T, typename... V>
struct dimension<std::vector<T, V...>>{
    static constexpr std::size_t value = 1 + dimension<T>::value;
};

int main() {
    std::cout << dimension<std::vector<std::vector<int>>>::value << std::endl;
}
#包括
#包括
模板
结构维度{static constexpr std::size\u t value=0;};
模板
结构维度{
静态constexpr std::size\u t value=1+维度::value;
};
int main(){
std::cout定义“什么是容器”有点难。下面检查
value\u type
iterator
、和
const\u iterator
嵌套的typedefs。根据需要调整
void\u t
检查。(例如,如果您只希望可以订阅的东西被识别为容器,那么添加
decltype。)(std::declval()[0])
添加到列表中。)

请注意,
dimension\u impl
的专门化调用
dimension
。这允许您专门化
dimension
,以处理不希望被视为容器的容器类对象(
std::string

模板结构维度;
命名空间详细信息{
模板
结构维度{
静态constexpr std::size\u t值=0;
};
模板
结构维度{
静态constexpr std::size\u t value=1+维度::value;
};
}
模板
结构维度:std::integral_常量{};

这也是我的想法。它必须被约束。@T.C.对。修复。这样你就可以报告
std::vector
的维度为2。@skypjack对我来说很好。
列表
是一个容器。如果有人只想准确识别
std::vector
std::array
,他们就有你的answer.(考虑到有无数类似于
向量的容器,我质疑这样做的有用性。)我得到的问题是-我想找到向量链或数组链的维度。你可以允许包含这两种类型的链(以及所有其他容器)。这是OP想要的吗?我想我的问题措辞错误,因为我试图说得非常具体。这两个答案对我的原始应用程序都很有价值,这与这个人工模拟的向量示例大不相同。
template<typename T>
struct dimension { static constexpr std::size_t value = 0; };

template<typename T, typename... V>
struct dimension<std::vector<T, V...>>{
    static constexpr std::size_t value = 1 + dimension<T>::value;
};
template<typename>
struct dimension { static constexpr std::size_t value = 0; };

template<typename T, std::size_t N>
struct dimension<std::array<T, N>>{
    static constexpr std::size_t value = 1 + dimension<T>::value;
};
#include<vector>
#include<iostream>

template<typename T>
struct dimension { static constexpr std::size_t value = 0; };

template<typename T, typename... V>
struct dimension<std::vector<T, V...>>{
    static constexpr std::size_t value = 1 + dimension<T>::value;
};

int main() {
    std::cout << dimension<std::vector<std::vector<int>>>::value << std::endl;
}
template<class T> struct dimension;

namespace details {    
    template<class T, class = void>
    struct dimension_impl {
        static constexpr std::size_t value = 0;
    };

    template<class T>
    struct dimension_impl<T, std::void_t<typename T::value_type,
                                         typename T::iterator,
                                         typename T::const_iterator>> {
        static constexpr std::size_t value = 1 + dimension<typename T::value_type>::value;
    };
}

template<class T>
struct dimension : std::integral_constant<std::size_t, 
                                          details::dimension_impl<T>::value> {};