Hi everybody.
I'm interested in C++ programming, so I just started learning it. I already worked with structured programming, e.g. Pascal or Fortran, so I have no problems with while, for, pointers and so on. Above all I'm interested in object-oriented programming, but I'm an absolute beginner in that. I'd like to write a little programm for work in such way, but I'm very confused. I have a definite aim and I searched for tutorials or examples about it but there's a lot of different libraries and different codes (often a mix of C and C++). Can you help me with a simple and basic code, so I can learn through it?
This is the question. I want to read a text file and manipulate it to obtain a different one. The structure of the file is a matrix with 3 columns and an unknown amount of rows (cartesian coordinates). Rows are grouped and empty lines split them. I have to copy the text file into a matrix, add a fourth 'flag' column and delete the empty lines. The flag consists in a integer: it's 1 for rows following an empty line and 0 for the other rows. The flag is 9 for the last row. An example:
-8.25000 0.00000 5.00000
-8.25000 2.40000 5.00000
-8.25000 3.14041 5.02089
-8.25000 3.88838 5.09037
-8.25000 4.26046 5.14651
-8.25000 4.62873 5.21863
-8.25000 4.99130 5.30800
-8.25000 5.34627 5.41589
-8.25000 5.69174 5.54358
-8.25000 6.02580 5.69235
-8.25000 6.34657 5.86347
-8.25000 6.65214 6.05822
-8.25000 6.94062 6.27787
-8.25000 7.21010 6.52370
-8.25000 7.45869 6.79699
-8.25000 7.68448 7.09900
-8.25000 7.68448 13.00000
-6.91500 0.00000 4.86198
-6.91500 0.53300 4.86107
-6.91500 1.52750 4.84723
-6.91500 2.52103 4.85241
-6.91500 3.24041 4.90173
-6.91500 3.95730 5.00000
-6.91500 4.65508 5.14855
-6.91500 5.33864 5.35470
-6.91500 5.67153 5.48129
-6.91500 5.99659 5.62458
-6.91500 6.31240 5.78533
-6.91500 6.61753 5.96431
-6.91500 6.67352 6.00000
-6.91500 7.01575 6.23832
-6.91500 7.34042 6.50144
-6.91500 7.64348 6.78862
-6.91500 7.92086 7.09914
-6.91500 7.92086 9.00000
-6.91500 7.92086 13.00000
0.00000 0.00000 3.97457
0.00000 0.73587 3.97467
0.00000 1.77104 4.00000
0.00000 2.45523 4.02717
0.00000 3.13497 4.09151
0.00000 3.47509 4.15067
0.00000 3.81199 4.22868
0.00000 4.14536 4.32388
0.00000 4.47487 4.43462
0.00000 5.12109 4.69613
0.00000 5.74813 5.00000
0.00000 6.32759 5.31955
0.00000 6.88956 5.66551
0.00000 7.38561 6.00000
0.00000 8.12791 6.53821
0.00000 8.85263 7.09970
0.00000 8.85263 9.00000
0.00000 8.85263 13.00000
is transformed into:
-8.25000 0.00000 5.00000 1
-8.25000 2.40000 5.00000 0
-8.25000 3.14041 5.02089 0
-8.25000 3.88838 5.09037 0
-8.25000 4.26046 5.14651 0
-8.25000 4.62873 5.21863 0
-8.25000 4.99130 5.30800 0
-8.25000 5.34627 5.41589 0
-8.25000 5.69174 5.54358 0
-8.25000 6.02580 5.69235 0
-8.25000 6.34657 5.86347 0
-8.25000 6.65214 6.05822 0
-8.25000 6.94062 6.27787 0
-8.25000 7.21010 6.52370 0
-8.25000 7.45869 6.79699 0
-8.25000 7.68448 7.09900 0
-8.25000 7.68448 13.00000 0
-6.91500 0.00000 4.86198 1
-6.91500 0.53300 4.86107 0
-6.91500 1.52750 4.84723 0
-6.91500 2.52103 4.85241 0
-6.91500 3.24041 4.90173 0
-6.91500 3.95730 5.00000 0
-6.91500 4.65508 5.14855 0
-6.91500 5.33864 5.35470 0
-6.91500 5.67153 5.48129 0
-6.91500 5.99659 5.62458 0
-6.91500 6.31240 5.78533 0
-6.91500 6.61753 5.96431 0
-6.91500 6.67352 6.00000 0
-6.91500 7.01575 6.23832 0
-6.91500 7.34042 6.50144 0
-6.91500 7.64348 6.78862 0
-6.91500 7.92086 7.09914 0
-6.91500 7.92086 9.00000 0
-6.91500 7.92086 13.00000 0
0.00000 0.00000 3.97457 1
0.00000 0.73587 3.97467 0
0.00000 1.77104 4.00000 0
0.00000 2.45523 4.02717 0
0.00000 3.13497 4.09151 0
0.00000 3.47509 4.15067 0
0.00000 3.81199 4.22868 0
0.00000 4.14536 4.32388 0
0.00000 4.47487 4.43462 0
0.00000 5.12109 4.69613 0
0.00000 5.74813 5.00000 0
0.00000 6.32759 5.31955 0
0.00000 6.88956 5.66551 0
0.00000 7.38561 6.00000 0
0.00000 8.12791 6.53821 0
0.00000 8.85263 7.09970 0
0.00000 8.85263 9.00000 0
0.00000 8.85263 13.00000 9
I started to write pieces of codes but, as I wrote above, I'm very confused with libraries, classes and so on. I rely on your patience and your help, I guess it's a very simple question for a C++ expert. I need this code as a tutorial. I can't understand anything about
std::vector<double> row;
std::copy(std::istream_iterator<double>(ifs),
std::istream_iterator<double>(),
std::back_inserter(row))
or
std::istream &read(std::istream &stream) {
std::copy(std::istream_iterator<double>(stream),
std::istream_iterator<double>(),
std::back_inserter(xyz));
In the meantime I go on with studying C++. Thanks for your attention.