, most notably serialization via QDataStream().
The operator<<( QDataStream &, const QVector<T> & ) and operator>>( QDataStream &, QVector<T> & ) streaming operators are not members of the QVector<T> template class, they are external non-member template functions. You could quite easily implement these same operators that would act on the std::vector<T> class and achieve all the benefits of QDataStream without requiring any conversion of your existing STL code:
template < typename T >
{
s << v.size();
std::vector<T>::const_iterator it = v.begin();
std::vector<T>::const_iterator eit = v.end();
while ( it != eit )
s << *it++;
return s;
}
template < typename T >
{
size_t nItems;
s >> nItems;
v.resize( nItems );
std::vector<T>::iterator it = v.begin();
std::vector<T>::iterator eit = v.end();
while ( it != eit )
s >> *it++;
return s;
}
template < typename T >
QDataStream & operator<<( QDataStream & s, const std::vector<T> & v )
{
s << v.size();
std::vector<T>::const_iterator it = v.begin();
std::vector<T>::const_iterator eit = v.end();
while ( it != eit )
s << *it++;
return s;
}
template < typename T >
QDataStream & operator>>( QDataStream & s, std::vector<T> & v )
{
size_t nItems;
s >> nItems;
v.resize( nItems );
std::vector<T>::iterator it = v.begin();
std::vector<T>::iterator eit = v.end();
while ( it != eit )
s >> *it++;
return s;
}
To copy to clipboard, switch view to plain text mode
And the like for any STL container class. Of course, operator>>() and operator<<() must be defined for whatever T is in the container, along with a default constructor for T.
Bookmarks