valarray<bool>

B

Busin

How to save a valarray<bool> object in a file and how to read it back into a
valarray<bool> object? Thanks!
 
J

John Harrison

How to save a valarray<bool> object in a file and how to read it back
into a
valarray<bool> object? Thanks!

It depends, first of all you have to decide what format you want to save
the information in. Do you want compactness, human readablity, or what
exactly?

There no way for me to answer that for you so I'll make some assumptions.
Suppose we save all the false values as the character '0' and all the true
values as the character '1'. Now it would be easy just to write out a
sequence of zeros and ones, but this would lead to a problem when you came
to read the valarray back in, namely how would you tell how big the
valarray was? So probably (but again its your choice) its best to first
write out the size of the valarray and then a space and then a series of
zeros and ones, and maybe a newline at the end just to make it more human
readable (but again this is your choice).

E.g.

valarray size 5, constisting of false, true, true, false, false would end
up being written out as

5 01100

Does this give you something to work with? Ask again if you don't know how
to code this, but have a go yourself first. If you were hoping for a 'one
line' answer then your out of luck, you are going to have to do some work
yourself.

BTW why are you using a valarray<bool> anyway? Is there a good reason to
prefer it over vector<bool>?

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,476
Latest member
blackwatermelon

Latest Threads

Top