"markww" <ma****@gmail.comwrote in message
news:11**********************@75g2000cwc.googlegro ups.com...
>
mlimber wrote:
>mlimber wrote:
markww wrote:
Gernot Frisch wrote:
"markww" <ma****@gmail.comschrieb im Newsbeitrag
news:11**********************@i3g2000cwc.googlegro ups.com...
Hi,
>
I have a data structure that looks like this:
>
struct MY_STRUCT {
int x,y,z;
string str1;
string str2;
vector<floatvfloats1;
vector<floatvfloats2;
std::ostream& operator << (std::ostream& os)
{
os << x << y << z << str1 << str1 << str2;
os << vfloats1.size();
for (int i=0; i<vflaots1.size; ++i) os << vfloats1[i];
os << vfloats2.size();
for (int i=0; i<vflaots2.size; ++i) os << vfloats2[i];
return os;
}
}
You might get into trouble with the strings, though, since some
delimiter characters as '\n' within a std::string can't be read in
this way.
Gernot, how would I read that back in?
As the FAQs say
(http://www.parashift.com/c++-faq-lit...html#faq-36.7),
you would use a similarly constructed extraction operator for a
std::istream.
PS, you'll need to add some sort of delimeter between elements, whether
a space, comma, new line, or whatever. Otherwise the unserializing
won't be possible.
Cheers! --M
Oh dear, this is going to be painful. If I have 10000 elements in my
float vectors, that's a lot of delimitation.
It's 1000 characters which isnt' much.
I would use spaces to delimit them since that works easily with reading them
back in.
Consider, also, that you don't actually need to store the size of the
vectors.
x y z
string1
string2
vec11 vec12 vec13 vec14 vec15
vec21 vec22 vec23 vec24 vec25 vec26 vec27
Now in your operator<< you read 3 ints, throw the rest of the line away.
read a line - store in string1
read a line - strong in string2
read a line - process through stringstream to get elements out
std::string InputString;
std::getline( Inputstream, InputString );
std::stringstream LineStream;
LineStream << InputString;
float Value;
while ( LineStream >Value )
vector1.push_back( Value );
there, that processes one vector. Now do the same for the second.
Personally, I find the delimitators that istream works with the easiest to
use.
>
Thanks guys,
Mark