Search code examples
c++xcodedictionarygmp

implementing map<mpz_t, double> in c++


For some reason, I need to have a map from arbitrary huge number to double and I tried to implement it with c++98 (and I have to) and Xcode but it doesn't work:

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include "gurobi_c++.h"
#include <sstream>
#include "boost/tuple/tuple.hpp"
#include "boost/tuple/tuple_comparison.hpp"
#include "boost/tuple/tuple_io.hpp"
#include <cmath>
#include <gmp.h>

using namespace std;
using namespace ::boost::tuples;
using namespace ::boost;

int main()
{
    map<mpz_t, double>J;
    mpz_t a,b,c,n;
    string tempstring;
    int xrange=5,yrange=5,component=5;

    mpz_set_str(n,"11", 10);
    J[n]=-1;

    return 0;
}

The error shown is: Array initializer must be an initializer list. Could someone help me with it? Thank you:)

Here's the detail error page: enter image description here enter image description here


Solution

  • I don't know the details of mpz_t. However, it appears to be an array.

    You can get around the problem by defining a class to be used as the key in your map.

    I am able to create an executable using the following code with g++ 4.8.2.

    #include <map>
    using namespace std;
    
    typedef int (mpz_t)[2];
    
    struct MyKey
    {
       // Add a proper implementation of a constructor
       // with mpz_t.
       MyKey(mpz_t in) {}
    
       // Add a proper implementation of copy constructor.
       MyKey(MyKey const& copy) {}
    
       // Add a proper implementation of assignment operator.
       MyKey& operator=(MyKey const& rhs)
       {
          return *this;
       }
    
       bool operator<(MyKey const& rhs) const
       {
          // Add a proper implementation.
          return false;
       }
    
       mpz_t n;
    };
    
    int main()
    {
       map<MyKey, double> J;
       mpz_t n;
       J[n] = 1.0;
       return 0;
    }