c++-wrapper and the vector class

Décio Luiz Gazzoni Filho decio at decpp.net
Sat Mar 4 22:10:02 CET 2006


On Mar 4, 2006, at 5:11 PM, Johan ... wrote:

> Hi!
>
> I've got a question for you. When working with a vector or an array  
> you
> adress the elements by writing  foo[x] right? Is it possible to  
> somehow
> rewrite the vectorclass or arrayclass in c++ so that the x becomes a
> mpz_class instead of an Int?
>
> Basically I want a really long array.

This has been asked in the past, and the answer is that you don't  
have enough memory for that. Don't try to argue this point, it's  
fruitless. Just think through until you convince yourself that this  
is a fact.

Now if you want a sparse array addressed by mpz_classes, i.e. an  
array where let's say there exists a value at foo[0], then at foo[5],  
then at foo[10^100], but only at these values and not in the  
intermediary values, then have a look at std::map. Do notice though  
that there'll be a performance penalty compared to an ordinary array  
-- an std::map is usually implemented as a binary tree, so accesses  
have cost O(log n), n being the number of valid elements in the array  
(3 in the example), instead of constant time as expected of an array.

Décio



More information about the gmp-discuss mailing list