Lire un fichier contenant une matrice et le stocker dans un vecteur
Bonjour,
Je voudrais lire à partir d'une fichier .txt une matrice triangulaire supérieure et la stocker dans une variable matrice. Est ce que quelqu'un aurait une idée sur la façon de s'y prendre. Je ne suis pas un expert en C++, mais je suis ouvert à toutes vos remarques qui peuvent me faire avancer.
Le fichier .txt à la configuration suivante :
Matrice de covariance
3.08787791981153E-02
6.5040824508478E-03 0.227357513661848
-3.30917216603529E-03 -1.79684729334368E-04 0.1
ect...
J'ai de mon côté trouver quelque chose qui tient la route mais je ne suis pas ur que ça me stocker ça dans une variable matrice d'autant plus que je ne sais pas si ça peut marcher pour une matrice triangulaire supérieur je vous donne la partie du code que j'ai trouvé:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| #include <vector> //pour std::vector
#include <string> // pour std::string et std::getline
#include <sstream> //pour std::istreamstring
#include <iostream> //pour les std::cout, std::cin....
#include <fstream> //pour lire les fichiers
std::vector<std::vector<int> > MatriceA;
std::ifstream fichier("toto.txt");
if(fichier)
{
std::string tmpLine;
while(std::getline(fichier,tmpLine))
{
std::vector<int> tmpVector;
std::istringstream toto(tmpLine);
std::string mot;
while ( std::getline(toto, mot, ';' ) )
{
std::istringstream mot_nombre(mot);
int nombre;
mot_nombre >> nombre;
tmpVector.push_back(nombre);
}
correlation.push_back(tmpVector);
}
////-- Display Matrice:
//std::vector<std::vector<int> >::iterator ita = MatriceA.begin();
//std::vector<int>::iterator ita2;
//while(ita != MatriceA.end())
//{
// ita2 = ita->begin();
// while(ita2 != ita->end())
// {
// std::cout<<"\t" <<*ita2;
// ita2++;
// }
// std::cout<<std::endl;
// ita++;
//}
////-- Or display again with []
//-- get indice I and indice j
std::vector<std::vector<int> >::iterator ita = MatriceA.begin();
std::vector<int>::iterator ita2;
long xlen =0;
long ylen =0;
if(ita !=MatriceA.end())
{
ylen=MatriceA.size(); //Y axis
xlen =MatriceA[0].size(); //X axis
}
for(long i =0; i<ylen; i++)
{
for(long j=0; j<xlen; j++)
{
std::cout<<"\t" <<MatriceA[j];
}
std::cout <<std::endl;
}
}
std::cin.ignore();
return 0; |