| Autor |
Nachricht |
HumeSikkins
Mitglied
Benutzerprofil
Anmeldungsdatum: 30.08.2000
Beiträge: 11139
|
HumeSikkins Mitglied
16:43:00 26.05.2002 Titel: |
## Einmal Zahl nach String und zurück |
Zitieren |
Wie wandelt man eigentlich eine Zahl in einen String?
Und wie macht man aus einem String eine Zahl?
Beide Fragen sollten eigentlich durch diesen Beitrag beantwortet werden:
a) Zahl nach String:
In Standard-C++ hat man drei verschiedene Möglichkeiten um eine Zahl
in einen String umzuwandeln.
Eine genaue Beschreibung der verschiedenen Möglichkeiten samt ihrer Vor- und
Nachteile findet man hier.
In Kürze:
---------
1. std::ostringstream
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 | #include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
double d = 3.14;
ostringstream Str;
Str << d;
string ZahlAlsString(Str.str());
cout << ZahlAlsString << endl;
} | |
2. std::ostrstream
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <strstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
double d = 3.14;
char buffer[10];
ostrstream Str(buffer, 10);
Str << d << ends;
string ZahlAlsString(Str.str());
cout << ZahlAlsString << endl;
} | |
3. sprintf
| C++: | 1 2 3 4 5 6 7 8 9 10 | #include <stdio.h>
#include <stdlib.h>
int main()
{
double d = 3.14;
char buffer[10];
sprintf(buffer, "%g", d);
printf("%s\n", buffer);
system("pause");
} | |
Zurück gibt es sogar noch mehr verschiedene Wege:
b) String nach Zahl
1. std::stringstream
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <sstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string AlsString("3.14");
stringstream Str;
Str << AlsString;
double d;
Str >> d;
cout << d << endl;
} | |
2. std::strstream
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #include <strstream>
#include <string>
#include <iostream>
using namespace std;
int main()
{
string AlsString("3.14");
char buffer[10];
strstream Str(buffer, 10);
Str << AlsString << ends;
double d;
Str >> d;
cout << d << endl;
} | |
3. atof, atoi, atol
| C++: | 1 2 3 4 5 6 7 8 9 | #include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
const char* AlsString = "3.14";
double d = atof(AlsString);
cout << d << endl;
} | |
4. sscanf
| C++: | 1 2 3 4 5 6 7 8 9 10 11 | #include <cstdio>
#include <iostream>
using namespace std;
int main()
{
const char* AlsString = "3.14";
float d = 0;
// ACHTUNG! sscanf erwartet ein float. Kein double!
sscanf(AlsString, "%g", &d);
cout << d << endl;
} | |
5. strtod, strtol, strtoul
| C++: | 1 2 3 4 5 6 7 8 9 10 11 | #include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main()
{
string AlsString("3.145");
char* StopPosition = 0;
double d = strtod(AlsString.c_str(), &StopPosition);
cout << d << endl;
} | |
C++11
| C++: | 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 | #include <string>
#include <iostream>
using namespace std;
int main() {
// double -> string
{
double const d = 0.5;
string s = to_string(d);
cout << s << endl;
}
// int -> string
{
int const i = 10;
string s = to_string(i);
cout << s << endl;
}
// string -> int (dezimal)
{
string const s = "10";
int i = stoi(s);
cout << i << endl;
}
// string -> int (hex)
{
string const s = "0x10";
int i = stoi(s, nullptr, 16);
cout << i << endl;
}
// string -> double
{
string const s = "0.5";
double d = stod(s);
cout << d << endl;
}
// Enderkennung
{
string const s = "10keinezahl";
size_t n;
int i = stoi(s, &n);
cout << i << endl;
if(n != s.size()) {
cout << n << " Zeichen als Zahl erkannt. Übrig: '" << s.substr(n) << "'\n";
}
}
} | | |
_________________ Remember Sturgeon's Law:
"Ninety percent of everything is crap."
and now go visit my Homepage ;-)
Zuletzt bearbeitet von pumuckl am 09:31:00 19.06.2012, insgesamt 4-mal bearbeitet |
|
 |
Werbeunterbrechung
|
|
 |
|
Nächstes Thema anzeigen
Vorheriges Thema anzeigen
Sie können keine Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum nicht antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.
|
|
|
|
|