Construction From vector<char> ?

Marc Glisse marc.glisse at
Tue Aug 12 20:58:31 CEST 2008

On Tue, 12 Aug 2008, Robert Evans wrote:

> I am trying to migrate  some  C++ code based on STL vector<char>  - based
> arrays to mpz_class.  Is there an efficient way to construct  an
> mpz_class object from a vector<char> without explicitly iterating over
> the vector?  I could not find  a  constructor  based on an STL vector,
> nor could I find a string constructor based  on vector<char> (which I
> could then feed into mpz_class(const string&) ).

It all depends on what the vector<char> is supposed to represent. If it 
was used in place of a string (as a list of the ascii representations of 
the digits of a base 10 representation of the number), you can easily 
create a string from your vector v, with:

string s(v.begin(),v.end());
or with:
string s(&v[0],v.size());
(the second one was not legal in early C++)

Note that the internal representation of a long integer in gmp is a C 
equivalent to vector<unsigned long> so the meaning of a vector is not 
something clear without specifying what it is supposed to represent.

Marc Glisse

More information about the gmp-discuss mailing list