473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

stream to file a matrix

bob
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

Mar 6 '06 #1
11 1901
bo*@blah.com 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::ostream& operator<<(std::ostream& s, const product& p) {
std::copy(p.begin(), p.end(),
std::ostream_iterator<std::string>(s, ", "));
return s;
}

std::ostream& operator<<(std::ostream& s, const product_matrix& p) {
std::copy(p.begin(), p.end(),
std::ostream_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...
Mar 6 '06 #2
bo*@blah.com 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::ostream& operator(std::ostream& out,
std::vector<std::string> const& v)
std::copy(v.begin(), v.end(),
std::ostream_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()'.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 6 '06 #3
bo*@blah.com 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.

Mar 6 '06 #4
Dietmar Kuehl wrote:
[..] On the other
hand, something like the following works in practice but it is not
guaranteed to work:

namespace std {
std::ostream& operator(std::ostream& out,
Was it supposed to be

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

? Or did my newsreader eat the "less-than" signs?
std::vector<std::string> const& v)
std::copy(v.begin(), v.end(),
std::ostream_iterator<std::string>(out, ","));
return out;
}
[...]


V
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #5
bo*@blah.com 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.
Mar 6 '06 #6
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::ostream& operator(std::ostream& out,


Was it supposed to be

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

?


Yes. I should probably try to compile the code instead of just
typing it in the newsreader... Thank you for catching this error.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 6 '06 #7
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::ostream_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.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 6 '06 #8
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::ostream_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...
Mar 6 '06 #9

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::ostream_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.

Mar 8 '06 #10
Fei Liu wrote:
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::ostream_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'?


Which part you didn't understand? I'm not qualified to give you a
lecture in English and thus I assume you understood the statement
per se (otherwise use a dictionary).

On a technical level, 'std::ostream_iterator<T>' is a template and
look-up of names in separated into two phases which is thus called
two-phase-look-up. For the dependent names, i.e. those depending on
the template arguments, only the second phase, i.e. argument dependent
look-up, applies. The rules for this are that only namespace are
considered in which one of the arguments is defined (the specification
of this are tricky but just collecting all namespace qualifiers of the
fully qualified names of the arguments will to the trick). The only
namespace involved in you class is 'std', i.e. this is the only
namespace in which names are sought.
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?


It is a restriction imposed to protect the library implementation
from being hosed up by people defining more or less arbitrary
functions in namespace 'std': as a user of the standard C++ library
you are only allowed to define certain functions in namespace 'std'
and when you are allowed to define a function the signature has to at
least contain one user defined type.
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.


I suggested to disobey the rules and define a function in namespace
'std' which are, strictly speaking, not allowed to define in
namespace 'std'. This is likely to work but the standard does not
give you any guarantee that it indeed will work because this violates
a specific restriction. These restrictions are there to protect the
library implementations: it is pretty easy to put functions into
namespace 'std' which will almost certainly cause the library to fail
in funny way. Also, compilation might easily fail if the library
implementor choose to put similar or even the same function into his
implementation.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Mar 8 '06 #11

Dietmar Kuehl wrote:
Fei Liu wrote:
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::ostream_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'?


Which part you didn't understand? I'm not qualified to give you a
lecture in English and thus I assume you understood the statement
per se (otherwise use a dictionary).

On a technical level, 'std::ostream_iterator<T>' is a template and
look-up of names in separated into two phases which is thus called
two-phase-look-up. For the dependent names, i.e. those depending on
the template arguments, only the second phase, i.e. argument dependent
look-up, applies. The rules for this are that only namespace are
considered in which one of the arguments is defined (the specification
of this are tricky but just collecting all namespace qualifiers of the
fully qualified names of the arguments will to the trick). The only
namespace involved in you class is 'std', i.e. this is the only
namespace in which names are sought.
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?


It is a restriction imposed to protect the library implementation
from being hosed up by people defining more or less arbitrary
functions in namespace 'std': as a user of the standard C++ library
you are only allowed to define certain functions in namespace 'std'
and when you are allowed to define a function the signature has to at
least contain one user defined type.
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.


I suggested to disobey the rules and define a function in namespace
'std' which are, strictly speaking, not allowed to define in
namespace 'std'. This is likely to work but the standard does not
give you any guarantee that it indeed will work because this violates
a specific restriction. These restrictions are there to protect the
library implementations: it is pretty easy to put functions into
namespace 'std' which will almost certainly cause the library to fail
in funny way. Also, compilation might easily fail if the library
implementor choose to put similar or even the same function into his
implementation.


Thank you, I didn't know there exists such kind of limitation on
implementing function in namespace 'std'. Too many rules...

Mar 9 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sarah | last post by:
I would like my vb.net software running on an independent system be able to read the data going from a proprietary system to a dot-matrix printer. Few questions - are there any commercially...
2
by: jjiyunlee | last post by:
Hi, I'm new to C (and programming in general), and I have to say that this site has helped me learn a great deal about C (thanks everybody!!). I've looked through several discussions specifically...
1
by: Michael | last post by:
I have a solution for this, but it feels wrong. If anyone could offer a better one, I'm all ears. (Or technically, eyes.) Basically, I have a bunch of classes. For concreteness, one is a...
10
by: bodowpin | last post by:
Hello. I am trying to read a text file that contains 1 2 3 it just looks like that. I was able to read it and assign each number to a matrix element (or array element). It was reading it all...
103
by: aboxylica | last post by:
hey! I have a program that takes two input files(one in the matrix form) and one in the sequence form.Now my problem is that i have to give the matrix file(containing many matrices) and sequence...
3
by: craziileeboi | last post by:
Hi I have been pulling my hair out trying to figure this out. Please help!!! Here is my project description: By using a pointer to pointers **A and **B and the function calloc() allocate...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
23
by: bc90021 | last post by:
Hi All, Thanks in advance for any and all help! I have this code: g = open(fileName, 'a') where fileName is defined before the line it's used in. It works fine when I use it outside a...
5
by: slizorn | last post by:
hi, well this is the file i have to read into the system... <matrix> rows = 2 cols = 2 1 2 2 4 </matrix>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.