如何知道我的容器包了几层?
文章目录
今天看到一个有趣的问题,请见下述例子:
cppvector<vector<int> > v2d; vector<vector<vector<string>> > v3d; vector<vector<vector<vector<double>>>> v4d;
如果给你 v2d、v3d 或是 v4d,你如何求得它们所包裹的层数?
万能的模板又要上场了:
cpptemplate<class Y> struct s { enum {dims = 0}; }; template<class Y> struct s<std::vector<Y>> { enum {dims = s<Y>::dims + 1}; };
来测试一下吧:
cppstd::vector<std::vector<double> > x; int n = s<decltype(x)>::dims; // n == 2;
这都属于 “乍看不难,细思恐极,模板棒喝” 的典型工程问题。。。
PS:搞图形的同志们应该会用得上~
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论