Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/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++ 如何使用decltype获取向量元素';s类型作为模板参数_C++_Templates_Decltype_Template Argument Deduction - Fatal编程技术网

C++ 如何使用decltype获取向量元素';s类型作为模板参数

C++ 如何使用decltype获取向量元素';s类型作为模板参数,c++,templates,decltype,template-argument-deduction,C++,Templates,Decltype,Template Argument Deduction,以下是我的部分代码: int getLength(const vector<int> &arr) { auto n=arr.size(),dis=n; unordered_map<int,decltype(dis)> S; //... } int getLength(常量向量和arr){ 自动n=arr.size(),dis=n; 无序地图; //... } 到目前为止还不错。现在,我不再为我的std::unordered_map硬编

以下是我的部分代码:

int getLength(const vector<int> &arr) {
    auto  n=arr.size(),dis=n;
    unordered_map<int,decltype(dis)> S;
    //...
}
int getLength(常量向量和arr){
自动n=arr.size(),dis=n;
无序地图;
//...
}
到目前为止还不错。现在,我不再为我的
std::unordered_map
硬编码“
int
”,而是尝试将其更改为如下内容:
无序地图
无序地图
无序地图

似乎都不管用。在这里使用
decltype()
的正确语法是什么

在这里使用decltype的正确语法是什么

decltype(arr.front())
decltype(arr[0])
都正常,但不幸的是,它们都将引用返回到常量
int
(考虑到
arr
是一个常量向量)

举例来说,您必须删除引用和常量

std::unordered_map<
      std::remove_const_t<std::remove_reference_t<decltype(arr.front())>>,
      decltype(dis)> S;
   std::unordered_map<
      std::remove_reference_t<decltype(arr)>::value_type,
      decltype(dis)> S;