c ++ CSV文件到2D数组不能正确输出

时间:2017-03-20 01:26:44

标签: c++ arrays csv

我有一个小问题,理解为什么我在打印2D数组时没有得到正确的输出。我正在从CSV文件中填充2D数组,我可以在加载后打印数组时得到正确的输出,但是第二次打印它时似乎出了问题。如果有人能解释为什么我得到相同的斑点指数值[100] [4]& index [100] [3]非常感谢。此外,当我在加载到数组后直接使用注释行打印数组时,我得到正确的输出。谢谢克里斯。 cout<< index [100] [4]<< ENDL; line包含值0.90949而在for循环中打印整个数组包含值0.85475。解释为什么会发生这种情况会非常有帮助。

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h> 
#include <string.h>
#include <sstream>
 using namespace std;

 int main() 
 {
    string line;
    string count;
    int linenumber = 0;

    ifstream myfile("iris-binary-normalized.csv");
    while(getline(myfile,count))
     ++linenumber;
     float index[linenumber][4];
     myfile.clear();
     myfile.seekg(0,myfile.beg);
     for(int row = 1; row < linenumber+1; ++row) {
         string x;
         getline(myfile,x);
     if( !myfile.good() )
             break;
     stringstream iss(x);
     for(int col = 1; col < 5; ++col) {
             string val;
             getline(iss, val, ',');
             if(!iss.good()) 
                     break;
             stringstream convertor(val);
             convertor >> index[row][col];
             //cout << index[row][col] << endl;

     }
 }   

   cout << index[100][4];


 return 0;

 }
 for(int i=1; i < linenumber+1; i++) {
       for(int j=0; j < 5; j++) {
            cout << index[i][j] << endl;
       }
 }

输出:

index [1][1] =-0.57815
index [1][2] =0.83762
index [1][3] =-1.0079
index [1][4] =-1.0369
index [2][1] =-0.88983
index [2][2] =-0.20679
index [2][3] =-1.0079
index [2][4] =-1.0369
index [3][1] =-1.2015
index [3][2] =0.21097
index [3][3] =-1.0769
index [3][4] =-1.0369
index [4][1] =-1.3573
index [4][2] =0.0020888
index [4][3] =-0.93891
index [4][4] =-1.0369
index [5][1] =-0.73399
index [5][2] =1.0465
index [5][3] =-1.0079
index [5][4] =-1.0369
index [6][1] =-0.11064
index [6][2] =1.6731
index [6][3] =-0.80094
index [6][4] =-0.683
index [7][1] =-1.3573
index [7][2] =0.62874
index [7][3] =-1.0079
index [7][4] =-0.85994
index [8][1] =-0.73399
index [8][2] =0.62874
index [8][3] =-0.93891
index [8][4] =-1.0369
index [9][1] =-1.669
index [9][2] =-0.41568
index [9][3] =-1.0079
index [9][4] =-1.0369
index [10][1] =-0.88983
index [10][2] =0.0020888
index [10][3] =-0.93891
index [10][4] =-1.2138
index [11][1] =-0.11064
index [11][2] =1.2554
index [11][3] =-0.93891
index [11][4] =-1.0369
index [12][1] =-1.0457
index [12][2] =0.62874
index [12][3] =-0.86993
index [12][4] =-1.0369
index [13][1] =-1.0457
index [13][2] =-0.20679
index [13][3] =-1.0079
index [13][4] =-1.2138
index [14][1] =-1.8248
index [14][2] =-0.20679
index [14][3] =-1.2149
index [14][4] =-1.2138
index [15][1] =0.5127
index [15][2] =1.882
index [15][3] =-1.1459
index [15][4] =-1.0369
index [16][1] =0.35687
index [16][2] =2.7176
index [16][3] =-0.93891
index [16][4] =-0.683
index [17][1] =-0.11064
index [17][2] =1.6731
index [17][3] =-1.0769
index [17][4] =-0.683
index [18][1] =-0.57815
index [18][2] =0.83762
index [18][3] =-1.0079
index [18][4] =-0.85994
index [19][1] =0.35687
index [19][2] =1.4643
index [19][3] =-0.80094
index [19][4] =-0.85994
index [20][1] =-0.57815
index [20][2] =1.4643
index [20][3] =-0.93891
index [20][4] =-0.85994
index [21][1] =-0.11064
index [21][2] =0.62874
index [21][3] =-0.80094
index [21][4] =-1.0369
index [22][1] =-0.57815
index [22][2] =1.2554
index [22][3] =-0.93891
index [22][4] =-0.683
index [23][1] =-1.3573
index [23][2] =1.0465
index [23][3] =-1.2838
index [23][4] =-1.0369
index [24][1] =-0.57815
index [24][2] =0.41985
index [24][3] =-0.80094
index [24][4] =-0.50606
index [25][1] =-1.0457
index [25][2] =0.62874
index [25][3] =-0.66297
index [25][4] =-1.0369
index [26][1] =-0.73399
index [26][2] =-0.20679
index [26][3] =-0.86993
index [26][4] =-1.0369
index [27][1] =-0.73399
index [27][2] =0.62874
index [27][3] =-0.86993
index [27][4] =-0.683
index [28][1] =-0.42232
index [28][2] =0.83762
index [28][3] =-0.93891
index [28][4] =-1.0369
index [29][1] =-0.42232
index [29][2] =0.62874
index [29][3] =-1.0079
index [29][4] =-1.0369
index [30][1] =-1.2015
index [30][2] =0.21097
index [30][3] =-0.86993
index [30][4] =-1.0369
index [31][1] =-1.0457
index [31][2] =0.0020888
index [31][3] =-0.86993
index [31][4] =-1.0369
index [32][1] =-0.11064
index [32][2] =0.62874
index [32][3] =-0.93891
index [32][4] =-0.683
index [33][1] =-0.42232
index [33][2] =2.0909
index [33][3] =-0.93891
index [33][4] =-1.2138
index [34][1] =0.045193
index [34][2] =2.2998
index [34][3] =-1.0079
index [34][4] =-1.0369
index [35][1] =-0.88983
index [35][2] =0.0020888
index [35][3] =-0.93891
index [35][4] =-1.0369
index [36][1] =-0.73399
index [36][2] =0.21097
index [36][3] =-1.1459
index [36][4] =-1.0369
index [37][1] =0.045193
index [37][2] =0.83762
index [37][3] =-1.0769
index [37][4] =-1.0369
index [38][1] =-0.88983
index [38][2] =1.0465
index [38][3] =-1.0079
index [38][4] =-1.2138
index [39][1] =-1.669
index [39][2] =-0.20679
index [39][3] =-1.0769
index [39][4] =-1.0369
index [40][1] =-0.57815
index [40][2] =0.62874
index [40][3] =-0.93891
index [40][4] =-1.0369
index [41][1] =-0.73399
index [41][2] =0.83762
index [41][3] =-1.0769
index [41][4] =-0.85994
index [42][1] =-1.5132
index [42][2] =-1.669
index [42][3] =-1.0769
index [42][4] =-0.85994
index [43][1] =-1.669
index [43][2] =0.21097
index [43][3] =-1.0769
index [43][4] =-1.0369
index [44][1] =-0.73399
index [44][2] =0.83762
index [44][3] =-0.86993
index [44][4] =-0.32911
index [45][1] =-0.57815
index [45][2] =1.4643
index [45][3] =-0.66297
index [45][4] =-0.683
index [46][1] =-1.0457
index [46][2] =-0.20679
index [46][3] =-1.0079
index [46][4] =-0.85994
index [47][1] =-0.57815
index [47][2] =1.4643
index [47][3] =-0.86993
index [47][4] =-1.0369
index [48][1] =-1.3573
index [48][2] =0.21097
index [48][3] =-1.0079
index [48][4] =-1.0369
index [49][1] =-0.26648
index [49][2] =1.2554
index [49][3] =-0.93891
index [49][4] =-1.0369
index [50][1] =-0.73399
index [50][2] =0.41985
index [50][3] =-1.0079
index [50][4] =-1.0369
index [51][1] =2.3827
index [51][2] =0.21097
index [51][3] =1.2687
index [51][4] =1.0864
index [52][1] =1.4477
index [52][2] =0.21097
index [52][3] =1.1307
index [52][4] =1.2634
index [53][1] =2.2269
index [53][2] =0.0020888
index [53][3] =1.4066
index [53][4] =1.2634
index [54][1] =0.045193
index [54][2] =-1.669
index [54][3] =0.78576
index [54][4] =0.90949
index [55][1] =1.6036
index [55][2] =-0.62456
index [55][3] =1.1997
index [55][4] =1.2634
index [56][1] =0.35687
index [56][2] =-0.62456
index [56][3] =1.1307
index [56][4] =0.90949
index [57][1] =1.2919
index [57][2] =0.41985
index [57][3] =1.2687
index [57][4] =1.4403
index [58][1] =-0.88983
index [58][2] =-1.4601
index [58][3] =0.30285
index [58][4] =0.37866
index [59][1] =1.7594
index [59][2] =-0.41568
index [59][3] =1.1997
index [59][4] =0.90949
index [60][1] =-0.42232
index [60][2] =-0.83344
index [60][3] =0.71677
index [60][4] =1.0864
index [61][1] =-0.73399
index [61][2] =-2.2956
index [61][3] =0.44083
index [61][4] =0.37866
index [62][1] =0.66854
index [62][2] =-0.20679
index [62][3] =0.92374
index [62][4] =1.2634
index [63][1] =0.82437
index [63][2] =-1.8779
index [63][3] =0.78576
index [63][4] =0.37866
index [64][1] =0.98021
index [64][2] =-0.41568
index [64][3] =1.2687
index [64][4] =1.0864
index [65][1] =0.20103
index [65][2] =-0.41568
index [65][3] =0.50981
index [65][4] =0.90949
index [66][1] =1.9152
index [66][2] =0.0020888
index [66][3] =1.0617
index [66][4] =1.0864
index [67][1] =0.20103
index [67][2] =-0.20679
index [67][3] =1.1307
index [67][4] =1.2634
index [68][1] =0.5127
index [68][2] =-0.83344
index [68][3] =0.85475
index [68][4] =0.37866
index [69][1] =1.136
index [69][2] =-1.8779
index [69][3] =1.1307
index [69][4] =1.2634
index [70][1] =0.20103
index [70][2] =-1.2512
index [70][3] =0.71677
index [70][4] =0.5556
index [71][1] =0.66854
index [71][2] =0.21097
index [71][3] =1.3377
index [71][4] =1.7942
index [72][1] =0.98021
index [72][2] =-0.62456
index [72][3] =0.78576
index [72][4] =0.90949
index [73][1] =1.2919
index [73][2] =-1.2512
index [73][3] =1.4066
index [73][4] =1.2634
index [74][1] =0.98021
index [74][2] =-0.62456
index [74][3] =1.2687
index [74][4] =0.73254
index [75][1] =1.4477
index [75][2] =-0.41568
index [75][3] =0.99272
index [75][4] =0.90949
index [76][1] =1.7594
index [76][2] =-0.20679
index [76][3] =1.0617
index [76][4] =1.0864
index [77][1] =2.0711
index [77][2] =-0.62456
index [77][3] =1.3377
index [77][4] =1.0864
index [78][1] =1.9152
index [78][2] =-0.20679
index [78][3] =1.4756
index [78][4] =1.6173
index [79][1] =0.82437
index [79][2] =-0.41568
index [79][3] =1.1307
index [79][4] =1.2634
index [80][1] =0.35687
index [80][2] =-1.0423
index [80][3] =0.44083
index [80][4] =0.37866
index [81][1] =0.045193
index [81][2] =-1.4601
index [81][3] =0.64779
index [81][4] =0.5556
index [82][1] =0.045193
index [82][2] =-1.4601
index [82][3] =0.5788
index [82][4] =0.37866
index [83][1] =0.5127
index [83][2] =-0.83344
index [83][3] =0.71677
index [83][4] =0.73254
index [84][1] =0.82437
index [84][2] =-0.83344
index [84][3] =1.5446
index [84][4] =1.4403
index [85][1] =-0.11064
index [85][2] =-0.20679
index [85][3] =1.1307
index [85][4] =1.2634
index [86][1] =0.82437
index [86][2] =0.62874
index [86][3] =1.1307
index [86][4] =1.4403
index [87][1] =1.9152
index [87][2] =0.0020888
index [87][3] =1.2687
index [87][4] =1.2634
index [88][1] =1.2919
index [88][2] =-1.669
index [88][3] =1.0617
index [88][4] =0.90949
index [89][1] =0.20103
index [89][2] =-0.20679
index [89][3] =0.85475
index [89][4] =0.90949
index [90][1] =0.045193
index [90][2] =-1.2512
index [90][3] =0.78576
index [90][4] =0.90949
index [91][1] =0.045193
index [91][2] =-1.0423
index [91][3] =1.0617
index [91][4] =0.73254
index [92][1] =0.98021
index [92][2] =-0.20679
index [92][3] =1.1997
index [92][4] =1.0864
index [93][1] =0.5127
index [93][2] =-1.0423
index [93][3] =0.78576
index [93][4] =0.73254
index [94][1] =-0.73399
index [94][2] =-1.669
index [94][3] =0.30285
index [94][4] =0.37866
index [95][1] =0.20103
index [95][2] =-0.83344
index [95][3] =0.92374
index [95][4] =0.90949
index [96][1] =0.35687
index [96][2] =-0.20679
index [96][3] =0.92374
index [96][4] =0.73254
index [97][1] =0.35687
index [97][2] =-0.41568
index [97][3] =0.92374
index [97][4] =0.90949
index [98][1] =1.136
index [98][2] =-0.41568
index [98][3] =0.99272
index [98][4] =0.90949
index [99][1] =-0.57815
index [99][2] =-1.2512
index [99][3] =0.095892
index [99][4] =0.5556
index [100][1] =0.35687
index [100][2] =-0.62456
index [100][3] =0.85475
index [100][4] =0.85475

0 个答案:

没有答案