This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.
Related
This question already has answers here:
Are there types bigger than long long int in C++?
(10 answers)
Closed 6 months ago.
I know that The maximum value for a variable of type unsigned long long in C++ is:
18,446,744,073,709,551,615
but I don't know that how can I set value of a variable to an amount that is more than 18,446,744,073,709,551,615?
maybe you can use __int128,the only problem is that you can't use cin,cout or printf but you can write a function like that:
//output
inline void write(__int128 x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
//input
inline __int128 read()
{
__int128 X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=x*10+(ch-'0'),ch=getchar();
return w?-X:X;
}
In vanilla C++ you can't, however, you can use Boost's multi-precision library.
This question already has answers here:
Does C++ support Variable Length Arrays?
(4 answers)
Why does a C/C++ compiler need know the size of an array at compile time?
(6 answers)
Closed 5 years ago.
void merge(vector<Flight>& data, int low, int high, int mid, string criteria)
{
int i, j, k, temp[high - low + 1];
...
The error that comes up is "the value of parameter "high" (declared at line 100) cannot be used as a constant". I haven't managed to find an appropriate answer to this question online.
high - low + 1 needs to be a compile time evaluable constant expression in C++. (C++ does not support variable length arrays.)
And that isn't, so the compiler issues a diagnostic.
The simple solution is to use a std::vector<int> as the type for temp.
This question already has answers here:
Is it possible to create a type in c++ that takes less than one byte of memory?
(5 answers)
Closed 5 years ago.
I require a 4 bit integer in a design for less memory use. In any version of c++ ,c++11 ,c++14 any can be used for the design.
There is no native 4bit datatype. But you could use an 8bit one to hold two 4bit values in the high/low nibble.
no, but you can use:
struct A {
unsigned int value1 : 4;
unsigned int value2 : 4;
};
This question already has answers here:
unsigned int vs. size_t
(8 answers)
Closed 6 years ago.
I saw an example recently that looks like the following:
const size_t NDim = 3;
double coords[NDim];
My question is straight forward. When does one use size_t vs an int or unsigned int? In this particular case, wouldn't the following be the equivalent as the above:
const unsigned int NDim = 3;
double coords[NDim];
size_t is commonly used for array indexing and loop counting.
According to cppreference:
Programs that use other types, such as unsigned int, for array indexing may fail on, e.g. 64-bit systems when the index exceeds UINT_MAX or if it relies on 32-bit modular arithmetic.
It also states:
std::size_t can store the maximum size of a theoretically possible
object of any type (including array). A type whose size cannot be
represented by std::size_t is ill-formed (since C++14)
The answer is straightforward as well. You use size_t for all your array indexing and sizing needs, this is exactly what it was designed for. And you never use anything else for it.
Apart from being a self-documenting feature, it also has another important aspect - on many platforms sizeof(int) is not equal to sizeof(size_t).
This question already has answers here:
Representing big numbers in source code for readability?
(5 answers)
Closed 7 years ago.
In C++, sometimes you want to declare large numbers. Sometimes it's hard to see if you have the right number of zeroes.
const long long VERY_LARGE_NUMBER = 300000000000;
In a language like OCaml, you can separate numbers with underscores to improve readability.
let x = 300_000_000_000;;
Is there a similar mechanism in C++? I have seen things like = 1 << 31 for powers of 2, but what about for very large powers of 10? Sometimes you're declaring very large numbers (e.g. array bounds in competition programming) and you want to be confident that your declared array size is correct.
I can think of something like:
const long long VERY_LARGE_NUMBER = 3 * (1 << (11 * 10 / 3));
...which abuses 1<<10 ~= 1000 get close to 3 with 11 zeroes, but it's verbose and not exact.
how about
const long long VERY_LARGE_NUMBER = (long long) 300 * 1000 * 1000 * 1000;
Since C++14, integer literal supports the use of ' as a delimiter. For example, unsigned long long l2 = 18'446'744'073'709'550'592llu;. See this cppreference page for the details. Also, you may consider using scientific notation, like 123e4. Such literals are floating point literals. But you can convert them to integer types.