Manipulation in stringstreams



  • Hallo,

    eine ganz simple Frage:

    #include <string>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    
    int main()
    {
    int h = 0, m = 9, s = 4;
    
    std::stringstream ss;
    
    ss << h << ':' << m << ':' << s;
    
    return 0;
    }
    

    Welche Manipulatoren muss ich hier einsetzen, damit die Zeit auf 2 Stellen auf Nullen aufgefüllt wird?
    Ich habe schon ein wenig mit ein paar Manipulatoren rumgespielt, aber das hat nicht ganz geklappt. 🙄

    Danke im Voraus,

    MfG MAV



  • setw(breite) // Ausgabebreite einstellen
    setfill('0') // 0 als Füllzeichen verwenden

    Mußt vor jeder Ausgabe erneut einstellen

    ss << setw(2) << setfill('0') << h << ':' << setw(2) << setfill('0') << m << ':' << setw(2) << setfill('0') << s;

    MfG Jester



  • Genau!

    VIELEN DANK 🙂


Anmelden zum Antworten