Manipulating 2d C++ arrays in a custom library - getting the number of rows? -
I am used for Java but learning C ++ and as an experiment I have to create a library Trying to file in a 2D array with the CSV user defined import specs, print the array in the console, and then type the array into a different file.
I create the array of rows (y-axis), print the array, and write the array while loop through 2d array. I have found the value of X-axis, and I'm negligible with it:
int y = sizeof (arr) / sizeof (arr [0]); Try to get it when I tested the code in a compiled CPP main, but now I got the correct number of rows correctly, but now I connected the function to one Is taken to the library which is assigning it Y to 0!
Why the difference, and how can I solve it? In the example below I am testing a matrix with 6 fields and 4 records, so I'm looking at 4 as the assigned value of 4.
Since I have narrowed the problem to this variable, here it is one (hopefully) how I am getting the SSCCE.
Header file: #ifndef csv_io_h # defines CSV_IO_H # and includes; String & gt; using namespace std; Class csv_io {public: static zero csv_to_dbl_arr (string, double arr [] [6], four, four); Static zero print_dbl_arr (double arr [] [6]); }; #endif Library included: #include "csv_io.h" #include & lt; Stdlib.h & gt; # Include & lt; Iostream & gt; # Include & lt; Fstream & gt; # Include & lt; Sstream & gt; # Include & lt; Iomanip & gt; using namespace std; Zero csv_io :: csv_to_dbl_arr (string INF, double ARR [] [6], four dlm, four sep) {ifstream ifs; String line, call, well; Int i = 0; Int j = 0; Ifs.open (inf.c_str ()); If (ifs.is_open ()) {while (ifs.good ()) {getline (ifs, line, sep); Stringstream call (line); While (col.good ()) {getline (col, val, dlm); Arr [i] [j] = etf (val.c_str ()); J ++; } J = 0; I ++; }} Ifs.close (); } Zero csv_io :: print_dbl_arr (double arr [] [6]) {int x = sizeof (arr [0]) / size (double); Int y = sizeof (arr) / sizeof (ARE [0]); Cout & lt; & Lt; "X:" & lt; & Lt; X & LT; & Lt; Andal & lt; & Lt; "Y:" & lt; & Lt; Y & lt; & Lt; Endl; } Main.cpp : #include "csv_io.h" #include "csv_io.cpp" #include & Lt; Stdlib.h & gt; using namespace std; Int main () {double d_arr [4] [6]; String inf = "file_i.txt"; Four in_dlm = ','; Four in_sep = '\ n'; Csv_io :: csv_to_dbl_arr (inf, d_arr, in_dlm, in_sep); Csv_io :: print_dbl_arr (d_arr); Return 0; } file_i.txt: 1,1,1,1,1,1,2,2,2, 2,2,2,3,3,3,3,4,4,4,4,4,4 I know that I have a counter & amp; Track the number of rows in Csv_to_dbl_arr but I have to express that I am using for Wi, so I can use it without declaring it mainly when I fly it.
zero print_dbl_arr (double arr [] [6]) is equal to void print_dbl_arr (double (* ARM) [6]) like
-
sizeof (arr) an indicator Size -
sizeof (arr [0]) size is double (& amp;) [6] hence 6 * size double . You can use the array from templates and references to get the shape as the following:
template & lt; Std :: size_t N, std :: size_t M & gt; Zero print_dbl_arr (double (& amp; ar) [n] [m]); Using std :: vector / std :: array is more intuitive.
Comments
Post a Comment