Daniel T. skrev:
I cant help wondering, why not just use deques instead of vectors all
the time?
At one point, even the likes of Herb Sutter was asking that question.
<
http://www.gotw.ca/gotw/054.htm> AFAICT, vector is used by default
because the standard says to use it by default (despite the fact that
they use deque by default in the standard adapters like stack.)
I have severel projects up and running, here is one of them, what they
all have in commen is that they work with wery large number and lots of
them.
Here is a prime generator i made, i have reasently chances some thing
and i have tested it, but it should work.
[Interesting prime code snipped]
You don't need a deque, or any other kind of container for prime
numbers. Primes are already a sequence, and you can represent them with
an iterator. I've already done this, but I'm not going to show my code,
better for you to do it yourself. Here is the interface though:
class primes:
public std::iterator< std::forward_iterator_tag, unsigned long >
{
public:
primes(); // create a sentinel object
explicit primes(unsigned long m); // 'm' is how many primes
// that will be in the sequence
unsigned long operator*() const; // return the current prime
primes& operator++(); // advance to the next prime
primes operator++(int) { primes tmp(*this); ++(*this); return tmp; }
friend bool operator==(const primes& lhs, const primes& rhs);
// if one of the inputs is a sentinel and the other is at the end
// of its sequence, return true. Otherwise check to see if the
// two inputs are pointing to the same prime, and will have the
// same effect if advanced.
};
bool operator!=(const primes& lhs, const primes& rhs) {
return !(lhs == rhs);
}
With the above, you can generate your primes file without taking up lots
of memory:
int main() {
ofstream file("primes.txt");
copy(primes(/*number of primes to write*/), primes(),
ostream_iterator<unsigned long>(file, "\n"));
}
If you write the primes class right, the memory requirements of the
above will remain constant no matter how many primes you write to the
file.
There is severel things that have i have though about.
1)
'primes.txt' will get wery large wery quick, ofcourse it cant ba
avoidet, however if you take an excample like 1234567890 it can take up
as little or less as 4 byte i memory, while in the file it takes up 10
bytes. It is easy to understand why, the question is can you do
anything about it?
Open the file as binary. Reading and writing to it will be tricker
though. Look up the ifstream::read and ofstream::write functions.
(hint) I would use a union for this:
union Buffer {
char c[sizeof(unsigned long)];
unsigned long i;
};
2)
I am using 2 different types here (the rest is not important) unsigned
long and unsigned long long, the need to work the same, wether i
compile on dev-c++ like i do now, or use something else. I have, on
severel ecations been told that this is not the case. Thats one of the
reasons i want to define my own types from scrats, but i guess its not
as simple as it sounds.
They don't really need to work the same, you should write your code so
that it works no matter the size of the types (hint: lookup
"numeric_limits".)
Note, if you go with a binary file the same file won't necessarily work
on all platforms (hint: lookup "endian".) There are things you can do to
fix that...