c++ - Stxxl Vector 作为 std::vector 的替代品

标签 c++ c++11 vector stl stxxl

这是 Vector of pairs with generic vector and pair type, template of template 的后续.

我希望能够调用带有 std::vector 的方法或 stxxl:vector , 而 vector 的模板参数(x,y 对) 被指定。

具体来说,signatrue 方法可能如下所示:

template<typename t_x, typename t_y,
            template<typename, typename> class t_pair,
            template<typename...> class t_vector>
    method(t_vector<t_pair<t_x,t_y>> &v) 

不幸的是,当像这样指定签名时,不可能传递 stxxl:vector作为t_vector .它会导致以下编译错误:

sad.hpp:128:5: note:   template argument deduction/substitution failed: 
program.cpp:104:52: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class t_vector’
         method(coordinates);
                           ^ 
program.cpp:104:52: error:   expected a type, got ‘4u’ 
program.cpp:104:52: error: type/value mismatch at argument 1 in template parameter list for ‘template<class ...> class t_vector’ 
program.cpp:104:52: error:   expected a type, got ‘2097152u’

问题是如何修改方法签名才能使用stxxl::vector使用 std::vector 作为现有代码的替代品?

更新为什么我要为 vector 使用嵌套模板: 我可能弄错了,但我希望编译器为上述方法中的变量类型。

例如,我正在构建一个 vectorqueue

std::vector<t_x> intervals(k * k + 1);
typedef std::tuple<std::pair<t_x,t_y>,std::pair<t_x,t_y>, std::pair<t_x,t_y>, uint> t_queue;
std::queue <t_queue> queue;

应该是 uint32_t or uint64_t取决于对元素的类型是 uint32_t or uint64_t

最佳答案

问题是 stxxl::vector 具有非类型模板参数:

BlockSize external block size in bytes, default is 2 MiB

所以它不能匹配 template <typename... > .

在这种情况下你不应该使用模板模板参数,这样会更好(我认为):

template <typename t_vector>
void method (t_vector &v) {
    typedef typename t_vector::value_type::first_type t_x;
    typedef typename t_vector::value_type::second_type t_y;
    // or in c++11 and above
    typedef decltype(v[0].first) t_xd;
    typedef decltype(v[0].second) t_yd;
}

在上面,您检索 t_xt_y要么使用:

  • value_type这是所有 Container 应该有(std::vectorstxxl::vector 都有);
  • decltype它直接从表达式 v[0].first 中获取类型(即使 v 为空也有效,因为 decltype 中的表达式从不计算)。

根据我的经验,最好使用一个非常通用的模板参数,然后从中检索信息(value_typedecltype、...),而不是试图用给定的类型来限制模板参数本身。

关于c++ - Stxxl Vector 作为 std::vector 的替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38430146/

相关文章:

c++ - std::sort - 给定一个自定义排序函数,是否可以在一次比较中进行多个排序查询?

C++ 使用 ofstream 写入二进制文件

c++ - Makefile 中扩展正则表达式列表的分隔符

c++ - 为什么即使删除了析构函数,我仍然可以创建匿名 union ?

python - 计算向量范数相对于python中向量的梯度

c++ - 如何在二维 vector 的定义位置插入一个 vector ?

C++ vector/链表混合

c++ - GDB 无法访问 mmap() 的内核分配内存?

C++ 二维数组查询

c++ - 从静态辅助函数访问私有(private)成员