wrote:
> given a vector of vectors of strings thus;
>
> typedef std::vector<std::string> product;
> typedef std::vector<product> product_matrix;
>
>
> whats the fastest, most efficient means of streaming the
> "product_matrix" to a file? I.E. without using two "for" or "while"
> loops. Is there a means of using copy ? I'd like to use stl's copy
> along with my 2 dimensional vector of strings. Is that possible? Or
> should I just iterate over the contents myself ?
'std::copy()' only works on sequences. There are several option,
however, to process a matrix as a sequence:
- You can have 'std::copy()' process each row of the matrix and
arrange for the rows to use 'std::copy()' internally - at least
this would be the case if you would use a user defined type: the
issue with this approach is that there is no output operator
defined for 'std::vector<T>' and you are only allowed to define
one if the type involves a user defined type somehow. On the other
hand, something like the following works in practice but it is not
guaranteed to work:
namespace std {
std:

stream& operator(std:

stream& out,
std::vector<std::string> const& v)
std::copy(v.begin(), v.end(),
std:

stream_iterator<std::string>(out, ","));
return out;
}
Now you can use 'std::copy()' with an appropriate output iterator
over 'product's.
- Instead of using 'std::copy()' you could use 'std::transform()'
using the above operator with an appropriate name and put it into
an appropriate namespace. This is, in some sense, the portable
alternative to the non-portable use of 'std::copy()'.
- You could create a special iterator which actually consists of
two iterators internally, one for the current row and one for the
current column within the row. This would give a kind of a "flat"
view of the matrix which an be used directly with 'std::copy()'.
--
<private.php?do=newpm&u=> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence