将mpfr_t对象作为值插入地图中

时间:2016-12-23 15:05:43

标签: c++ pointers memoization gmp mpfr

我遇到了插入std :: pair类型元素的麻烦,mpfr_t>进入地图。 std :: make_pair函数调用错误

将'__mpfr_struct *'赋值给'__mpfr_struct [1]'

的类型不兼容

由于我只是将一个指向mpfr_t对象的指针传递给rec_func函数,我认为我可以使用*运算符取消引用它并将生成的mpfr_t保存为映射中的值。

代码的背景信息和结构:rec_func是递归函数。它应该计算某些情况。因为实例数量非常大(10 ^ 50或更多),我使用的数据类型为mpfr_t。为了避免使用相同的参数多次调用递归函数,我想使用动态编程(又名Memoization)。为此,我使用带有三个整数作为键的向量的映射,并使用mpfr_t作为值。

main函数初始化地图和mpfr_t类型的对象。然后它调用rec_func并在地图上移动并指向mpfr_t对象。因为mpfr_t实际上是一个数组类型,所以它不能被返回,因此我只是传递一个指向它的指针。

首先需要安装gmp和mfpr库。 sudo apt-get install libgmp3-dev sudo apt-get install libmpfr-dev libmpfr-doc libmpfr4 libmpfr4-dbg

非常感谢建议。

#include <map>
#include <vector>
#include <iostream>
#include <gmp.h>
#include <mpfr.h>
using namespace std;

void rec_func(std::map <std::vector<int>, mpfr_t>& my_map, mpfr_t*    big_int)
{

  int arr[3] = {1, 2, 3}; 
  std::vector<int> itm(arr, arr+3);

  std::pair <std::vector<int>, mpfr_t> intr;
  intr = std::make_pair(itm, *big_int);
  //my_map.insert(intr);

  //my_map.insert ( std::make_pair(itm, *big_int) ); 

}

int main()
{
  mpfr_t big_int; // initialize
  mpfr_init2(big_int, 200);
  mpfr_set_d(big_int, 1, MPFR_RNDN); // assign value

  std::map <std::vector<int>, mpfr_t> my_map;

  rec_func(my_map, &big_int);

  mpfr_clear(big_int); // clear the big int
  my_map.clear(); // delete the map

}

1 个答案:

答案 0 :(得分:0)

mpfr_t类型不是一个简单的结构,事实上,作为mpfr.h标题,它是一个大小为1的数组:

typedef __mpfr_struct mpfr_t[1];

这意味着您无法使用=复制它(就像在std::make_pair中一样)。

最简单的解决方案是使用指向mpfr_t的指针并将其存储在std::pair(和std::map)中。