gpt4 book ai didi

c++ - 使用以一对 int、int 和 int 为模板的映射实现内存表

转载 作者:行者123 更新时间:2023-11-30 01:27:45 26 4
gpt4 key购买 nike

我已经实现了一个递归算法,为了提高性能我想添加一个内存表。我的问题最自然的结构是

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];
}
}

这会引发大量的编译器错误。我不确定语法是否正确。我应该使用字符串缓冲区来创建一个字符串,然后使用它而不是对吗?

这里是编译错误:

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]

澄清一下,我不确定语法是否正确。我想知道如何使用一对作为映射的键。

额外信息:我问过this较早的问题。我现在正在优化它。请随意为内存表建议不同的结构。

最佳答案

取而代之的是

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_tablestd::map<std::pair<int,int>,int> , 它的键类型是 std::pair<int,int> , 所以索引为 lookup_table必须是它的关键,当你使用 []运营商。

std::make_pair是一个效用函数,它返回类型为 std::pair<int,int> 的对象如果 std::make_pair 的两个参数是int .而不是写作,std::make_pair , 你可以使用 std::pair<int,int>(row_index,colm_index)但这看起来很麻烦,这就是为什么 std::make_pair是首选。

关于c++ - 使用以一对 int、int 和 int 为模板的映射实现内存表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8413061/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com