Bonjour a tous ,
Dans ce code , je ne comprends pas ce que signifie :
flux.str(std::string {});
Peut etre pour afficher ?
std::string traitement_chaine(std::string const & chaine)
{
std::string copie { chaine };
auto premier_non_espace { std::find_if_not(std::begin(copie), std::end(copie), isspace) };
copie.erase(std::begin(copie), premier_non_espace);
std::reverse(std::begin(copie), std::end(copie));
premier_non_espace = std::find_if_not(std::begin(copie), std::end(copie), isspace);
copie.erase(std::begin(copie), premier_non_espace);
std::reverse(std::begin(copie), std::end(copie));
return copie;
}
.
std::ostream & operator<<(std::ostream & sortie, Artiste const & artiste)
{
sortie << artiste.nom;
return sortie;
}
std::ostream & operator<<(std::ostream & sortie, Album const & album)
{
sortie << album.nom;
return sortie;
}
std::ostream & operator<<(std::ostream & sortie, Morceau const & morceau)
{
sortie << morceau.nom << " | " << morceau.album << " | " << morceau.compositeur;
return sortie;
}
std::istream & operator>>(std::istream & entree, Morceau & morceau)
{
std::string mot {};
std::ostringstream flux {};
while (entree >> mot && mot != "|")
{
flux << mot << " ";
}
std::string nom_morceau { flux.str() };
if (std::empty(nom_morceau))
{
nom_morceau = "Morceau inconnu";
}
morceau.nom = traitement_chaine(nom_morceau);
flux.str(std::string {});
// Récupération du nom de l'album.
while (entree >> mot && mot != "|")
{
flux << mot << " ";
}
std::string nom_album { flux.str() };
if (std::empty(nom_album))
{
nom_album = "Album inconnu";
}
morceau.album.nom = traitement_chaine(nom_album);
flux.str(std::string {});
while (entree >> mot)
{
flux << mot << " ";
}
std::string nom_artiste { flux.str() };
if (std::empty(nom_artiste))
{
nom_artiste = "Artiste inconnu";
}
morceau.compositeur.nom = traitement_chaine(nom_artiste);
flux.str(std::string {});
return entree;
}
Et si je peux me permettre , je ne comprends pas quel est le role de :
assert
void test_creation_morceau_entree_complete()
{
std::istringstream entree{ "Frica | Frica | Carla's Dreams" };
Morceau morceau{};
entree >> morceau;
assert(morceau.nom == "Frica" && u8"Le nom du morceau doit être Frica.");
assert(morceau.album.nom == "Frica" && u8"Le nom de l'album doit être Frica.");
assert(morceau.compositeur.nom == "Carla's Dreams" && u8"Le nom du compositeur doit être Carla's Dreams.");
}