Comment déclarer une gamme de cordes de deux dimensions en C ++? Et aussi comment écrire cette chaîne sur les fichiers? P>
6 Réponses :
Vous pouvez déclarer une gamme multidimensionnelle de chaînes telles que ceci:
std::ofstream output("result.txt");
for (size_t i = 0; i < 137; ++i)
for (size_t j = 0; j < 42; ++j)
output << myArray[i][j] << std::endl;
faire sortie.close () code> à la fin.
Ce n'est pas nécessaire; Lorsque l'objet de flux dépasse de la portée, le destructeur fermera le fichier.
typedef std::vector<std::string> StringVector;
typedef std::vector<StringVector> StringVector2D;
StringVector2D twoD;
for (StringVector2D::iterator outer = twoD.begin(); outer != twoD.end(); ++outer)
for (StringVector::iterator inner = outer->begin(); inner != outer->end(); ++inner)
std::cout << *inner << std::endl;
Déclaration et initialisation Ensemble:
std::string myarray[2][3] = {
{ "hello", "jack", "dawson" },
{ "hello", "hello", "hello" }
};
Je suppose que vous avez le type qstring. Cela devrait fonctionner à STD :: String et même (Char *) correctement.
myTwoDimensionalArray[x][y] = "Hello, World!";
#include<iostream>
#include<vector>
using namespace std;
main()
{
vector< vector<string> > m2m;
vector<string> A, B;
vector< vector<string> >::iterator inter_i;
vector<string>::iterator inter_j;
A.push_back("micro");
A.push_back("soft");
A.push_back("bilgates");
B.push_back("linux");
B.push_back("unix");
B.push_back("ken dennish");
m2m.push_back(A);
m2m.push_back(B);
cout<<endl<<" USing iterator : "<<endl;
for(inter_i=m2m.begin();inter_i!=m2m.end();inter_i++)
{
for(inter_j=(*inter_i).begin();inter_j!=(*inter_i).end();inter_j++)
{
cout<<*inter_j<<" ";
}
cout<<endl;
}
return 0;
}
Ceci créera un objet de chaîne 2D de chaîne:
String str[no of strings];
Non, cela créera un tableau 1D de chaînes.
Quel genre de cordes? Littéraux strings? C Strings?
std :: chaîne code> objets?csching code> objets?qstring code> objets? Strings Unicode d'une certaine sorte? Cordes cryptées? Quelques autres types de chaînes? Quel type de fichier avez-vous besoin de les écrire? Doivent-ils être codés de manière particulière dans le fichier? Avez-vous Un bon livre d'introduction C ++ ? Si oui, l'avez-vous consulté? Sinon, vous devriez en avoir un.