2017-09-28 7 views
1

Wir haben eine Matrix in Textdatei wie folgt mit Kommas zwischen Zahlen, aber es gibt kein Komma am Ende jeder Zeile.Lesen Matrix aus Textdatei mit Vektoren in C++

 
1,2,3,4 
7,8,2,1 
3,4,5,6 
7,2,1,3 

Ich habe versucht, dies mit einem 2D-Array wie dies zu tun, aber es ist nicht wirklich funktioniert, weil auch die Größe der Matrix ist nicht bekannt.

Ich möchte das mit 2D-Vektoren tun, aber ich habe keine Ahnung, wie das geht Wie nach dem Erstellen eines Vektors mit

vector<vector<string>> matrix; 

Sollte ich einen weiteren Vektor innerhalb der Schleifen erstellen?

Antwort

0

Verwenden Sie eine doppelte std::vector, lesen Sie die Datei Zeile für Zeile, und analysieren Sie die Kommas auch, und Sie sind fertig. Jedes Mal, wenn Sie eine Zeile lesen, erhöhen Sie die Größe des Vektors um 1. Dies können Sie mit std::vector::resize() tun.

Beispiel, indem Sie Ihre Datei als "matrix.txt":

#include <iostream> 
#include <sstream> 
#include <string> 
#include <fstream> 
#include <vector> 

int main(void) { 
    std::vector<std::vector<int>> matrix; 
    std::ifstream infile("matrix.txt"); 
    int a, b, c, d; 
    char comma; 
    while (infile >> a >> comma >> b >> comma >> c >> comma >> d) 
    { 
     //std::cout << a << " " << b << " " << c << " " << d << std::endl; 
     matrix.resize(matrix.size() + 1); 
     matrix[matrix.size() - 1].push_back(a); 
     matrix[matrix.size() - 1].push_back(b); 
     matrix[matrix.size() - 1].push_back(c); 
     matrix[matrix.size() - 1].push_back(d); 
    } 

    for(auto row: matrix) { 
     for(auto v: row) { 
      std::cout << v << " "; 
     } 
     std::cout << "\n"; 
    } 
    return 0; 
} 

Ausgang:

Georgioss-MacBook-Pro:~ gsamaras$ g++ -std=c++0x main.cpp 
Georgioss-MacBook-Pro:~ gsamaras$ ./a.out 
1 2 3 4 
7 8 2 1 
3 4 5 6 
7 2 1 3 
+0

Dank aber wir wissen nicht, wie viele Kommas in txt-Datei ??? – rektandlove

+0

Sie wissen, dass jede Zeile in Ihrer Datei 4 Zahlen und 3 Kommata @rektandlove;) – gsamaras

+0

keine TXT-Datei kann 5x5 oder 3x3 ändern :( – rektandlove

1

Verwendung Vektor von Vektoren. Hier ist der Kommentar für jede Zeile:

std::vector<std::vector<int>> v;  // declare vector of vectors 
std::ifstream ifs("myfile.txt");  // open the file 
std::string tempstr;     // declare a temporary string 
int tempint;       // declare a temporary integer 
char delimiter;      // declare a temporary delimiter 
while (std::getline(ifs, tempstr)) { // read line by line from a file into a string 
    std::istringstream iss(tempstr); // initialize the stringstream with that string 
    std::vector<int> tempv;   // declare a temporary vector for the row 
    while (iss >> tempint) {   // extract the numbers from a stringstream 
     tempv.push_back(tempint);  // push it onto our temporary vector 
     iss >> delimiter;    // read the , delimiter 
    } 
    v.push_back(tempv);    // push the vector onto vector of vectors 
} 

Der vollständige Quellcode ist:

#include <iostream> 
#include <fstream> 
#include <vector> 
#include <sstream> 
#include <string> 

int main() { 
    std::vector<std::vector<int>> v; 
    std::ifstream ifs("myfile.txt"); 
    std::string tempstr; 
    int tempint; 
    char delimiter; 

    while (std::getline(ifs, tempstr)) { 
     std::istringstream iss(tempstr); 
     std::vector<int> tempv; 
     while (iss >> tempint) { 
      tempv.push_back(tempint); 
      iss >> delimiter; 
     } 
     v.push_back(tempv); 
    } 

    for (auto row : v) { 
     for (auto el : row) { 
      std::cout << el << ' '; 
     } 
     std::cout << "\n"; 
    } 
}