Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stream to file a matrix

Reply
Thread Tools

stream to file a matrix

 
 
bob@blah.com
Guest
Posts: n/a
 
      03-06-2006
Hi,

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 ?

thanks much

G

 
Reply With Quote
 
 
 
 
Ben Pope
Guest
Posts: n/a
 
      03-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> 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.


How do you intend to do that?

> Is there a means of using copy ?


Yes, you would essentially need to call copy for each element of
product_matrix, and then each element of the products.

> 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 ?


How do you think copy is implemented?

I would create a stream operator for product and product_matrix:


#include <vector>
#include <string>
#include <iostream>

typedef std::vector<std::string> product;
typedef std::vector<product> product_matrix;

std:stream& operator<<(std:stream& s, const product& p) {
std::copy(p.begin(), p.end(),
std:stream_iterator<std::string>(s, ", "));
return s;
}

std:stream& operator<<(std:stream& s, const product_matrix& p) {
std::copy(p.begin(), p.end(),
std:stream_iterator<product>(s, "\n"));
return s;
}

int main() {
product p(10, "test");
product_matrix m(10, p);

std::cout << m;
}


The only problem is that doesn't actually work. What did I do wrong?

Yes, there are nested loops, but there really is no way around that.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
 
 
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-06-2006
(E-Mail Removed) 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()'.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-06-2006
(E-Mail Removed) wrote:

> Hi,
>
> 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.


What makes you believe a loop is slow or inefficient?

> 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 ?


I don't think there is a way to use copy that way.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      03-06-2006
Dietmar Kuehl wrote:
> [..] 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,


Was it supposed to be

std:stream& operator << (std:stream& out,

? Or did my newsreader eat the "less-than" signs?

> std::vector<std::string> const& v)
> std::copy(v.begin(), v.end(),
> std:stream_iterator<std::string>(out, ","));
> return out;
> }
> [...]


V
--
Please remove capital As from my address when replying by mail
 
Reply With Quote
 
Jacek Dziedzic
Guest
Posts: n/a
 
      03-06-2006
(E-Mail Removed) wrote:
> Hi,
>
> 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 ?


What makes you think you need it in the first place?
I'd bet the I/O would be the bottleneck here in nine out
of ten cases. If you optimize the streaming, it will only
_wait more quickly_ for the I/O.

You didn't mention if you write the matrix in binary or
text format. The latter is usually slow when the streams
are used because of the extra overhead of conversion.

HTH,
- J.
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-06-2006
Victor Bazarov wrote:
> Dietmar Kuehl wrote:
>> [..] 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,

>
> Was it supposed to be
>
> std:stream& operator << (std:stream& out,
>
> ?


Yes. I should probably try to compile the code instead of just
typing it in the newsreader... Thank you for catching this error.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Dietmar Kuehl
Guest
Posts: n/a
 
      03-06-2006
Ben Pope wrote:
> I would create a stream operator for product and product_matrix:


Note, that this code is not supposed to compile! The problem is
that output operator used by 'std:stream_iterator<int>' is sought
only in namespace 'std'. However, since the involved types are all
built-in types, you are - strictly speaking - not allowed to define
the output operator there! Technically, it is likely to work if you
just plug the output operators into namespace 'std' but the
behavior of the program is not defined.
--
<(E-Mail Removed)> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
 
Reply With Quote
 
Ben Pope
Guest
Posts: n/a
 
      03-06-2006
Dietmar Kuehl wrote:
> Ben Pope wrote:
>> I would create a stream operator for product and product_matrix:

>
> Note, that this code is not supposed to compile! The problem is
> that output operator used by 'std:stream_iterator<int>' is sought
> only in namespace 'std'. However, since the involved types are all
> built-in types, you are - strictly speaking - not allowed to define
> the output operator there! Technically, it is likely to work if you
> just plug the output operators into namespace 'std' but the
> behavior of the program is not defined.


Thanks for that Dietmar.

I guess if you want to do this, it's best to wrap the types rather than
use a typedef.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      03-08-2006

Dietmar Kuehl wrote:
> Ben Pope wrote:
> > I would create a stream operator for product and product_matrix:

>
> Note, that this code is not supposed to compile! The problem is
> that output operator used by 'std:stream_iterator<int>' is sought
> only in namespace 'std'. However, since the involved types are all


what do you mean 'sought only in name space std'?

> built-in types, you are - strictly speaking - not allowed to define
> the output operator there! Technically, it is likely to work if you


I don't see the connection between built-in type and reason why output
operator is not allowed to be fined in 'std', can you elaborate on this
please?

> just plug the output operators into namespace 'std' but the
> behavior of the program is not defined.


Not sure about the meaning of 'plug the output operators into name
std'...And why is the program behavior not defined? The code seem to
compile/run perfectly.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Matrix operations on character matrix element? Terry Reedy Python 0 04-02-2009 12:12 AM
Matrix*Vector and Vector*Matrix Holgerson C++ 3 10-26-2007 07:38 AM
Matrix composed by two matrix lvcargnini VHDL 3 07-05-2006 07:21 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
Re: Matrix DTS and Matrix 2 DTS? PeterTHX DVD Video 0 08-03-2003 05:46 AM



Advertisments