J
Jens
Hi
Is there at class in C++ to make objectstreams or shall I make it yourself?
Thanks
Jens
Is there at class in C++ to make objectstreams or shall I make it yourself?
Thanks
Jens
Jens said:Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
Thanks
Jens
Jens said:Hi
Is there at class in C++ to make objectstreams or shall I make it
yourself?
HiCatalin said:Jens said: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 <typename E, typename T>
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.
Jens said:HiCatalin said:Jens said: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 <typename E, typename T>
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.
My problem is to write a vector<float> to a file and later read the file
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());
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.