473,780 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stream to file a matrix

bob
Hi,

given a vector of vectors of strings thus;

typedef std::vector<std ::string> product;
typedef std::vector<pro duct> 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 1934
bo*@blah.com wrote:
Hi,

given a vector of vectors of strings thus;

typedef std::vector<std ::string> product;
typedef std::vector<pro duct> 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<pro duct> product_matrix;

std::ostream& operator<<(std: :ostream& s, const product& p) {
std::copy(p.beg in(), p.end(),
std::ostream_it erator<std::str ing>(s, ", "));
return s;
}

std::ostream& operator<<(std: :ostream& s, const product_matrix& p) {
std::copy(p.beg in(), p.end(),
std::ostream_it erator<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<pro duct> 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::o stream& out,
std::vector<std ::string> const& v)
std::copy(v.beg in(), v.end(),
std::ostream_it erator<std::str ing>(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.co m> <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<pro duct> 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::o stream& 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.beg in(), v.end(),
std::ostream_it erator<std::str ing>(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<pro duct> 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::o stream& 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.co m> <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_i terator<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.co m> <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_i terator<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_i terator<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

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

Similar topics

3
1576
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 available or "hack" devices available that would allow me to tap into the data the proprietary system is sending to the dot matrix printer? How could I get my vb.net software to respond listen to and respond to this data stream? Are there any...
2
9356
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 about reading a matrix from a file, but I think (based on my admittedly small amount of knowledge about things like this) that my problem is slightly different. My for-loops for fscanf-ing values from a user-specified file seems to be ignored...
1
2599
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 Matrix class, but that's only one example, so please don't get too hung up on it. I need to output and input these classes. I'd like a nice, pretty, human readable output, something like: 1 2 3
10
7514
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 fine and I was trying to change the elements to int variables a,b,c so that I could say matrix = {a,b,c}; At some point during tweaking all the matrix elements became 0 and I have lost myself.
103
5897
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 file containing many sequences and calculate the same log score as I did for one matrix file and one sequence file. how it should exactly work is that. for every sequence it should calculate log values for all the weight matrices,then go to the...
3
2217
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 the memory for the 4x4 matrices A and B. By using the pointers *a and *b and the function malloc() allocate the memory for the 4-dimensional vectors a and b.
1
2672
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
1680
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 thread class.
5
2093
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
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10075
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9931
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6727
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5504
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2869
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.