2017-03-17 140 views
-1

所以我有一個文本文件(20×20矩陣)下面的數字,我需要將其加載到一個矩陣,所以我可以與他們合作:閱讀整數矩陣(C++)

problem11 .TXT

08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 

因爲我知道號碼存儲爲一個20×20矩陣,我可以將它們作爲:

int counter = 0; 
int num_of_col = 20, num_of_row = 20, init_value = 0; 
std::vector<std::vector<int>> numbers; 
numbers.resize(num_of_col, std::vector<int>(num_of_row, init_value)); 


std::ifstream infile("problem11.txt");  // open file 
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, a16, a17, a18, a19, a20; 
while (infile >> a1 >> a2 >> a3 >> a4 >> a5 >> a6 >> a7 >> a8 >> a9 >> a10 >> a11 >> a12 >> a13 
    >> a14 >> a15 >> a16 >> a17 >> a18 >> a19 >> a20){ 

    numbers[counter][0] = a1; 
    numbers[counter][1] = a2; 
    numbers[counter][2] = a3; 
    numbers[counter][3] = a4; 
    numbers[counter][4] = a5; 
    numbers[counter][5] = a6; 
    numbers[counter][6] = a7; 
    numbers[counter][7] = a8; 
    numbers[counter][8] = a9; 
    numbers[counter][9] = a10; 
    numbers[counter][10] = a11; 
    numbers[counter][11] = a12; 
    numbers[counter][12] = a13; 
    numbers[counter][13] = a14; 
    numbers[counter][14] = a15; 
    numbers[counter][15] = a16; 
    numbers[counter][16] = a17; 
    numbers[counter][17] = a18; 
    numbers[counter][18] = a19; 
    numbers[counter][19] = a20; 
    counter++; 
} 

雖然這似乎是非常低效的。如果我需要改變矩陣的大小,我必須手動改變一切。有人能以更聰明或者更有活力的方向指出我嗎?

+0

你好像知道'while'循環,那麼爲什麼不把另一個循環放到你的循環中來自動讀取列呢? – bejado

+1

[閱讀矩陣文本和存儲作爲數組](https://stackoverflow.com/questions/572962/reading-a-matrix-txt-file-and-storing-as -an-array) – HDJEMAI

+0

@ H.DJEMAI在這篇文章中提供的答案是不相似的 - 至少從我可以告訴。我對C++相當綠色。 – bgaard

回答

2

您不需要所有已聲明和使用的整數變量。

istream::getline()非常有用

  1. 打開文件.txt並將其命名爲infile。
  2. 聲明string mystr,您將存儲您的值
  3. 然後您使用getline(infile,mystr)getline()具有由缺省作爲分隔符的\n
  4. 現在getline存儲在mystr該文件的第一行。
  5. mystr您現在可以開始按值分隔值並將其存儲到第二個mystr2並將mystr2推入您的陣列。
  6. 如果你的數組是一個整數數組,你將不得不在C++ 11中使用stof,或者在C++ 98中使用stringstream將它們從字符串轉換爲整數。如果你的數組是一個字符串數組(char *),你可以立即推送它們。

第5步可以實現2策略。

1.getline()以「空間」作爲分隔符2.string::substrstring::erase削減所有的mystr,你不需要

這樣的部件是非常簡單 - 聰明並且不需要所有這些變量。它只需要兩個。

1
int num_of_col = 20, num_of_row = 20, init_value = 0; 
std::vector<std::vector<int>> numbers; 
numbers.resize(num_of_col, std::vector<int>(num_of_row, init_value)); 

std::ifstream infile("problem11.txt");  // open file 
for(int krow = 0; krow<num_of_row; krow++) 
{ 
    std::vector<int> vrow; 
    for(int kcol; kcol < num_of_col; kcol++) 
    { 
    int a; 
    infile >> a; 
    vrow.push_back(a); 
    } 
    numbers.push_back(row); 
} 
+0

我嘗試過類似的東西,並且這些值變成代數操作無法訪問,或者它們沒有按照它們應該加載的方式加載。在我的結尾,一切都輸出爲0.但是,謝謝你的方法,我一定會牢記在心。 – bgaard

+0

「代數操作無法訪問的值」我無法想象這可能意味着什麼,除了你必須有一個錯誤。要使用這種方法來瀏覽一些使用C++ 11處理矩陣的代碼,請查看https://github.com/JamesBremner/tsp – ravenspoint