K
Keith MacDonald
I've been trying to write generic procedures for persistence of the contents
of a standard collection. The output side works perfectly, but the input
side fails when the collection is of strings and a string contains
whitespace. The problem is that I don't know the type that's stored in the
collection, so can't treat strings as a special case. This is how I've
tried to solve the problem:
template <class COLL>
void readCollection(COLL& coll)
{
COLL::value_type elem;
while (MoreData()) {
std::basic_istringstream<char>(GetData()) >> elem;
coll.insert(coll.end(), elem);
}
}
Asuming GetData() returns a string from somewhere, this can be successfully
invoked as:
std::vector<int> v1;
readCollection< std::vector<int> >(v1);
However, the following only works if GetData() returns a string without
embedded whitespace:
std::vector<std::string> v2;
readCollection< std::vector<std::string> >(v2);
Otherwise, each element only gets the first word of each string. Clearly,
this is a case for unformatted input, but that will only work for strings.
How can I implement a generic solution?
Thanks,
Keith MacDonald
of a standard collection. The output side works perfectly, but the input
side fails when the collection is of strings and a string contains
whitespace. The problem is that I don't know the type that's stored in the
collection, so can't treat strings as a special case. This is how I've
tried to solve the problem:
template <class COLL>
void readCollection(COLL& coll)
{
COLL::value_type elem;
while (MoreData()) {
std::basic_istringstream<char>(GetData()) >> elem;
coll.insert(coll.end(), elem);
}
}
Asuming GetData() returns a string from somewhere, this can be successfully
invoked as:
std::vector<int> v1;
readCollection< std::vector<int> >(v1);
However, the following only works if GetData() returns a string without
embedded whitespace:
std::vector<std::string> v2;
readCollection< std::vector<std::string> >(v2);
Otherwise, each element only gets the first word of each string. Clearly,
this is a case for unformatted input, but that will only work for strings.
How can I implement a generic solution?
Thanks,
Keith MacDonald