I am creating a prime generator in c++ using an array to store primes as I find them and then use them to check against potentials later. Is there a way to "grow" my array as my program continues?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- How do I get from a type to the TryParse method?
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
I would use a std:vector something like this:
then you would add primes to it by using:
and retrieve values from the vector by using:
Good luck!
See std:vector. http://new.cplusplus.com/reference/stl/vector/
The usual cases where this sort of need arises also require an efficient lookup operation. My experience is to go for
std::map
which makes life much easier than vectors here.You could use a
std::vector
template, and use thereserve
method based on an upper-bound for the prime counting function.If there is any initially known limit to which you are going to go, you can just calculate the length of the array you need with this simple formula:
let
N
is that limit andLENGTH
is the needed length of the array, soLENGTH = N / log(N)
where the
log(N)
is a natural logarithm ofN
for more information, see: Prime number distribution theorem.
It sounds like you need a vector to store primes in.