 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jens Guest
|
Posted: Thu Oct 28, 2004 9:39 am Post subject: objectStream in C++? |
|
|
Hi
Is there at class in C++ to make objectstreams or shall I make it yourself?
Thanks
Jens
|
|
| Back to top |
|
 |
Catalin Pitis Guest
|
Posted: Thu Oct 28, 2004 9:57 am Post subject: Re: objectStream in C++? |
|
|
"Jens" <jens.cortsenremovethiis (AT) tiscali (DOT) dk> wrote
| Quote: | Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
Thanks
Jens
|
A common solution is to define streaming operators for stream in and out of
an object.
For example,
class A
{
[...]
template <typename E, typename T>
friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E, T>&
os, const A& obj)
{
os << obj.x;
return os;
}
template
friend std::basic_istream< E, T>& operator>>( std::basic_istream< E, T>&
is, A& obj)
{
is >> obj.x;
return is;
}
private:
int x;
};
You have in STL file streams, string streams and you can find other kind of
streams (for sockets for example).
the usage is very simple:
A obj1, obj2;
std::cout << obj1 << obj2;
Catalin
P.S. The code is just a sketch. It may not compile.
|
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Thu Oct 28, 2004 10:33 am Post subject: Re: objectStream in C++? |
|
|
Jens wrote:
| Quote: | Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
|
That depends on what an "objectstream" is.
|
|
| Back to top |
|
 |
Jens Guest
|
Posted: Thu Oct 28, 2004 11:27 am Post subject: Re: objectStream in C++? |
|
|
Catalin Pitis wrote:
| Quote: |
"Jens" <jens.cortsenremovethiis (AT) tiscali (DOT) dk> wrote in message
news:4180be42$0$93475$eprecisef (AT) dread15 (DOT) news.tele.dk...
Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
Thanks
Jens
A common solution is to define streaming operators for stream in and out
of an object.
For example,
class A
{
[...]
template <typename E, typename T
friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
T>&
os, const A& obj)
{
os << obj.x;
return os;
}
template
friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
T>&
is, A& obj)
{
is >> obj.x;
return is;
}
private:
int x;
};
You have in STL file streams, string streams and you can find other kind
of streams (for sockets for example).
the usage is very simple:
A obj1, obj2;
std::cout << obj1 << obj2;
Catalin
P.S. The code is just a sketch. It may not compile.
Hi |
My problem is to write a vector
into a vector<float>. Sorry I not where precise.
Jens
|
|
| Back to top |
|
 |
Catalin Pitis Guest
|
Posted: Thu Oct 28, 2004 11:35 am Post subject: Re: objectStream in C++? |
|
|
"Jens" <jens.cortsenremovethiis (AT) tiscali (DOT) dk> wrote
| Quote: | Catalin Pitis wrote:
"Jens" <jens.cortsenremovethiis (AT) tiscali (DOT) dk> wrote in message
news:4180be42$0$93475$eprecisef (AT) dread15 (DOT) news.tele.dk...
Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
Thanks
Jens
A common solution is to define streaming operators for stream in and out
of an object.
For example,
class A
{
[...]
template <typename E, typename T
friend std::basic_ostream< E, T>& operator<<( std::basic_ostream< E,
T>&
os, const A& obj)
{
os << obj.x;
return os;
}
template
friend std::basic_istream< E, T>& operator>>( std::basic_istream< E,
T>&
is, A& obj)
{
is >> obj.x;
return is;
}
private:
int x;
};
You have in STL file streams, string streams and you can find other kind
of streams (for sockets for example).
the usage is very simple:
A obj1, obj2;
std::cout << obj1 << obj2;
Catalin
P.S. The code is just a sketch. It may not compile.
Hi
My problem is to write a vector
into a vector<float>. Sorry I not where precise.
Jens
|
Here is some code
vector< float> vecToStore;
// ... initialize it
// Output to stream cout, with space delimiters
copy ( vecToStore.begin ( ), vecToStore.end ( ),
ostream_iterator<float> ( cout, " " ) );
vector< float> vecToLoad;
// Input from stream cin
copy( istream_iterator<float>( cin), istream_iterator<float>(),
vecToLoad.begin());
Again, it is just a sketch.
Catalin
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Thu Oct 28, 2004 12:20 pm Post subject: Re: objectStream in C++? |
|
|
| Quote: |
Here is some code
vector< float> vecToStore;
// ... initialize it
// Output to stream cout, with space delimiters
copy ( vecToStore.begin ( ), vecToStore.end ( ),
ostream_iterator<float> ( cout, " " ) );
vector< float> vecToLoad;
// Input from stream cin
copy( istream_iterator<float>( cin), istream_iterator<float>(),
vecToLoad.begin());
|
Should be
copy( istream_iterator<float>( cin), istream_iterator<float>(),
back_inserter(vecToLoad));
Also the code above uses a text representation of the floating point
numbers, which is good because its portable. But if you want a binary
representation, try this
vector< float> vecToStore;
vector< float>::size_type size = vecToStore.size();
out.write((char*)&size, sizeof size);
if (size > 0)
out.write((char*)&vecToStore[0], size*sizeof(float));
vector< float> vecToLoad;
vector< float>::size_type size;
in.write((char*)&size, sizeof size);
if (size > 0)
{
vecToLoad.resize(size);
in.write((char*)&vecToLoad[0], size*sizeof(float));
}
Again untested.
john
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|