In String, Variable + Text schreiben
-
Hey ist es möglich in einem String einen Text + Variable zu speichern?
z.B. so:string test = "Das Pokemon " << s_poke << " wurde soeben auserwaehlt!";
-
Schau dir mal
std::stringstream
an.
-
ja, mit stringstreams:
#include <sstream> #include <string> int main() { std::ostringstream StrStream; StrStream << "Das Pokemon " << s_poke << " wurde soeben auserwaehlt!"; std::string Test = StrStream.str(); }
-
Danke für die schnellen Antworten!
Nur meine Frage dazu nun:
Ich wollte mit einem Teamplate eine "Standartausgabe" machen.
Aber dann lohnt sich das mit dem Teamplate nicht mehr wenn ich denstd::stringstream
benutze oder?
//copyright© 2014 Tryoxie_Dev_Team #include <sstream> #include <iostream> #include <conio.h> #include <string> using namespace std; template<typename T> void ausgabe(T text){ cout << "--------------------------------------------------------------------------------"; cout << text << "\n"; cout << "--------------------------------------------------------------------------------"; } int main(){ cout << "------------------------\n"; cout << "Bitte Pokemon auswaehlen\n"; cout << "------------------------\n\n"; cout << "1. Shiggy (Wasser)\n"; cout << "2. Bisasam (Pflanze)\n"; cout << "3. Glumanda (Feuer)\n"; string s_poke; cin >> s_poke; if(s_poke != "Shiggy" && s_poke != "Bisasam" && s_poke != "Glumanda"){ cout << "Falsche eingabe!"; } else{ ostringstream StrStream; StrStream << "Das Pokemon " << s_poke << " wurde soeben auserwaehlt!"; string test = StrStream.str(); ausgabe(test); } getch(); return 0; }
-
Das geht mit einem Manipulator mit Parameter - etwa so:
struct pokemon_auswahl_type { pokemon_auswahl_type( const std::string& text ) : text_( text ) {} std::string text_; }; std::ostream& operator<<( std::ostream& out, const pokemon_auswahl_type& x ) { return out << "Das Pokemon " << x.text_ << " wurde soeben auserwaehlt!"; } pokemon_auswahl_type pokemon_auswahl( const std::string& s_poke ) { return pokemon_auswahl_type( s_poke ); }
Aufruf:
cout << pokemon_auswahl(s_poke) << endl;
oder auch als Template, wenn s_poke nicht vom Typ
std::string
ist.template< typename T > struct pokemon_auswahl_type { pokemon_auswahl_type( const T& text ) : text_( text ) {} T text_; // oder auch const T& text_; }; template< typename T > std::ostream& operator<<( std::ostream& out, const pokemon_auswahl_type< T >& x ) { return out << "Das Pokemon " << x.text_ << " wurde soeben auserwaehlt!"; } template< typename T > pokemon_auswahl_type< T > pokemon_auswahl(const T& s_poke) { return pokemon_auswahl_type< T >( s_poke ); }
Der Aufruf bleibt der gleiche.
-
es geht auch sehr einfach:
string mit Text verbinden:
std::string t ="Hallo"; t += " Welt";
oder dein Beispiel:
std::string t = std::string("TEXT") + variable + std::string("TEXT");
-
Sepultura schrieb:
std::string t = std::string("TEXT") + variable + std::string("TEXT");
Das klappt nur mit Strings oder Chars.
-
Nexus schrieb:
Sepultura schrieb:
std::string t = std::string("TEXT") + variable + std::string("TEXT");
Das klappt nur mit Strings oder Chars.
std::to_string