C++ 使用以int、int和int为模板的映射实现备忘录表
我已经实现了一个递归算法,为了提高性能,我想添加一个记忆表。我的问题最自然的结构是C++ 使用以int、int和int为模板的映射实现备忘录表,c++,map,memoization,C++,Map,Memoization,我已经实现了一个递归算法,为了提高性能,我想添加一个记忆表。我的问题最自然的结构是 map<pair<int,int>,int> lookup_table; 地图查找表; 我使用的递归算法是 int max_sum_path(int maximum_rows,vector<vector<int> >& matrix,int row_index,int colm_index) { if(row_index >= maximum_
map<pair<int,int>,int> lookup_table;
地图查找表;
我使用的递归算法是
int max_sum_path(int maximum_rows,vector<vector<int> >& matrix,int row_index,int colm_index)
{
if(row_index >= maximum_rows || colm_index > row_index)
{
//we have reached a cell outside the Triangular Matrix
return 0;
}
else if(lookup_table.find(row_index,colm_index) != lookup_table.end())
{
//the memoization step to avoid repeated calculations and make recursion more efficient
return lookup_table[row_index,colm_index];
}
else
{
lookup_table[row_index,colm_index] = matrix[row_index][colm_index] + max(max_sum_path(maximum_rows,matrix,row_index+1,colm_index), max_sum_path(maximum_rows,matrix,row_index+1,colm_index+1));
return lookup_table[row_index,colm_index];
}
}
int-max\u-sum\u路径(int-max\u行、向量和矩阵、int-row\u索引、int-colm\u索引)
{
if(行索引>=最大行索引>行索引)
{
//我们已经到达三角形矩阵外的一个单元
返回0;
}
else if(lookup\u table.find(row\u index,colm\u index)!=lookup\u table.end())
{
//记忆化步骤可避免重复计算并使递归更有效
返回查找表[行索引,列索引];
}
其他的
{
查找表[row_index,colm_index]=矩阵[row_index][colm_index]+max(最大和路径(最大和路径,矩阵,行索引+1,colm_索引),最大和路径(最大和路径,矩阵,行索引+1,colm_索引+1));
返回查找表[行索引,列索引];
}
}
这会引发大量编译器错误。我不确定语法是否正确。
我应该使用一个字符串缓冲区来创建一个字符串,然后使用它而不是一对吗
以下是编译器错误:
sums_triangle.cpp: In function ‘int max_sum_path(int, std::vector<std::vector<int> >&, int, int)’:
sums_triangle.cpp:50:49: error: no matching function for call to ‘std::map<std::pair<int, int>, int>::find(int&, int&)’
sums_triangle.cpp:50:49: note: candidates are:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:736:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = std::pair<int, int>, _Tp = int, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:736:7: note: candidate expects 1 argument, 2 provided
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:751:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) const [with _Key = std::pair<int, int>, _Tp = int, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:751:7: note: candidate expects 1 argument, 2 provided
sums_triangle.cpp:53:35: warning: left operand of comma operator has no effect [-Wunused-value]
sums_triangle.cpp:53:45: error: no match for ‘operator[]’ in ‘lookup_table[(0, colm_index)]’
sums_triangle.cpp:53:45: note: candidate is:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>, _Tp = int, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: no known conversion for argument 1 from ‘int’ to ‘const key_type& {aka const std::pair<int, int>&}’
sums_triangle.cpp:57:28: warning: left operand of comma operator has no effect [-Wunused-value]
sums_triangle.cpp:57:38: error: no match for ‘operator[]’ in ‘lookup_table[(0, colm_index)]’
sums_triangle.cpp:57:38: note: candidate is:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>, _Tp = int, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: no known conversion for argument 1 from ‘int’ to ‘const key_type& {aka const std::pair<int, int>&}’
sums_triangle.cpp:58:35: warning: left operand of comma operator has no effect [-Wunused-value]
sums_triangle.cpp:58:45: error: no match for ‘operator[]’ in ‘lookup_table[(0, colm_index)]’
sums_triangle.cpp:58:45: note: candidate is:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const key_type&) [with _Key = std::pair<int, int>, _Tp = int, _Compare = std::less<std::pair<int, int> >, _Alloc = std::allocator<std::pair<const std::pair<int, int>, int> >, std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int, std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::pair<int, int>]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../include/c++/4.6.2/bits/stl_map.h:445:7: note: no known conversion for argument 1 from ‘int’ to ‘const key_type& {aka const std::pair<int, int>&}’
sums_triangle.cpp:60:1: warning: control reaches end of non-void function [-Wreturn-type]
sums\u triangle.cpp:在函数“int max\u sum\u path(int,std::vector&,int,int)”中:
sums_triangle.cpp:50:49:错误:调用“std::map::find(int&,int&)”时没有匹配的函数
sums_三角形。cpp:50:49:注:候选人为:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_-map.h:736:7:注意:std::map::迭代器std::迭代器std::map::find(const-key_-type&)[带(key=std::pair,)Tp=int,(比较=std::less,)Alloc::Alloc=std::分配器,std::map::迭代器std::tree=std::)std:(Rb
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../../include/c++/4.6.2/bits/stl_-map.h:736:7:注意:候选者需要1个参数,提供2个参数
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_-map.h:751:7:注意:std::map::const_迭代器std::map::const_迭代器std::find(const key_-type&)const[带key=std::pair::pair,Tp=int,Compare=std::less,Alloc=std::Alloc::分配器,std::map::const::const迭代器std:;
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../../include/c++/4.6.2/bits/stl_-map.h:751:7:注意:候选者需要1个参数,提供2个参数
sums_triangle.cpp:53:35:警告:逗号运算符的左操作数无效[-Wunused value]
sums_triangle.cpp:53:45:错误:查找表[(0,colm_索引)]中的“运算符[]”不匹配
sums_三角形。cpp:53:45:注:候选人是:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_-map.h:445:7:注意:std::map::mapped_-type&std::map::operator[](const-key_-type&)[带(key=std::pair::pair,(Tp=int,)Compare=std::less,)Alloc=std::Alloc::分配器,std::map::mapped(type=int,std::key=std::pair]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_map.h:445:7:注意:参数1从“int”到“const key_type&{aka const std::pair&}的转换未知
sums_triangle.cpp:57:28:警告:逗号运算符的左操作数无效[-Wunused value]
sums_triangle.cpp:57:38:错误:查找表[(0,colm_索引)]中的“运算符[]”不匹配
sums_三角形。cpp:57:38:注:候选人为:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_-map.h:445:7:注意:std::map::mapped_-type&std::map::operator[](const-key_-type&)[带(key=std::pair::pair,(Tp=int,)Compare=std::less,)Alloc=std::Alloc::分配器,std::map::mapped(type=int,std::key=std::pair]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_map.h:445:7:注意:参数1从“int”到“const key_type&{aka const std::pair&}的转换未知
sums_triangle.cpp:58:35:警告:逗号运算符的左操作数无效[-Wunused value]
sums_triangle.cpp:58:45:错误:查找表[(0,colm_索引)]中的“运算符[]”不匹配
sums_三角形。cpp:58:45:注:候选人是:
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_-map.h:445:7:注意:std::map::mapped_-type&std::map::operator[](const-key_-type&)[带(key=std::pair::pair,(Tp=int,)Compare=std::less,)Alloc=std::Alloc::分配器,std::map::mapped(type=int,std::key=std::pair]
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.2/../../../../../../../include/c++/4.6.2/bits/stl_map.h:445:7:注意:参数1从“int”到“const key_type&{aka const std::pair&}的转换未知
sums_triangle.cpp:60:1:警告:控件到达非无效函数的末尾[-Wreturn类型]
只是想澄清一下,我不确定语法是否正确。我想知道如何使用一对作为地图的键
额外信息:
我早些时候问过这个问题。我现在正在优化它。请随意为备忘录表建议不同的结构。不幸的是,这不是python。 您不能使用以下语法:
lookup_table[row_index,colm_index]
您需要使用make_pair()创建配对:
lookup_table[std::make_pair(row_index,colm_index)]
除此之外,您可能应该使用从map.find()返回的迭代器来返回记忆结果。目前,您正在进行两次查找。不幸的是,这不是python。 您不能使用以下语法:
lookup_table[row_index,colm_index]
您需要使用make_pair()创建配对:
lookup_table[std::make_pair(row_index,colm_index)]
除此之外,您可能应该使用从map.find()返回的迭代器来返回记忆结果。现在,您将进行两次查找。而不是此
lookup_table.find(row_index,colm_index) //incorrect - your code
写这个,
lookup_table.find(std::make_pair(row_index,colm_index)) //correct - my code
同样地,我们也看到了这些
lookup_table[row_index,colm_index]; //incorrect - your code
lookup_table[std::make_pair(row_index,colm_index)]; //correct - my code
说明:
由于lookup\u table
是std::map
,它的键类型是std::pair
,因此当您使用[]
操作符时,lookup\u table
的索引必须是它的键
std::make_pair
是一个实用函数,返回