问题
I'm working with moderately sized integers which are of a size so that their logarithm would be an int. I'm using the GNU Multiprecision library (GMP) with the C programming language.
I wonder whether there exists a function that would convert the data type size_t into an int.
回答1:
Source: src size_t is implementation dependent yeah, and it's size may vary. You can try to typecast it to int but that may not be able to store as you already mentioned indirectly. But still you can work with strings
#include <iostream>
#include <string>
using namespace std;
int main()
{
size_t x = 123456789012345678LL;
std::cout<<(x)<<" "<<int(x)<<" "<<to_string(x);
And then use:
mpz_init(n);
mpz_set_ui(n,0);
flag = mpz_set_str(n,x, 10);
assert (flag == 0);
printf ("n = ");
mpz_out_str(stdout,10,n);
printf ("\n");
}
(Never read about GMP before this question, but the approach should be correct. Hope it helps :-)
来源:https://stackoverflow.com/questions/62504040/convert-size-t-to-integer