C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

string in Zahl umwandeln

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Tobias Wollgam
Guest





PostPosted: Thu Apr 29, 2004 9:35 am    Post subject: string in Zahl umwandeln Reply with quote



Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int die
Funktion atoi oder gibt es was besseres?

Sollte man sich eine eigene Funktion schreiben, die das macht?

z.B:

int toInt(const std::string& s)
{
return atoi(s.c_str());
}

.... damit man jederzeit intern Änderungen machen kann?

Vielleicht möchte man später mal bei einem String, der keine Zahl ist, eine
Exception werfen!?!

TIA,
Tobias

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Karl Jenz
Guest





PostPosted: Sat May 01, 2004 9:00 am    Post subject: Re: string in Zahl umwandeln Reply with quote



Tobias Wollgam schrieb:

Quote:
Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int
die Funktion atoi oder gibt es was besseres?


Hallo Tobias,

also unter CBuilder 1.0 gibt es
s.ToInt ()
und s.ToIntDef (Alternativzahl)

So etwas ähnliches müsste es in std::string auch geben.

Quote:
Sollte man sich eine eigene Funktion schreiben, die das macht?


Erscheint mir eher überflüssig.

--
Mfg Karl Jenz
http://karl.jenz.bei.t-online.de

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Rolf Magnus
Guest





PostPosted: Sat May 01, 2004 10:24 am    Post subject: Re: string in Zahl umwandeln Reply with quote



Tobias Wollgam wrote:

Quote:
Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int die
Funktion atoi oder gibt es was besseres?

std::ostringstream.

Quote:
Sollte man sich eine eigene Funktion schreiben, die das macht?

z.B:

int toInt(const std::string& s)
{
return atoi(s.c_str());
}

... damit man jederzeit intern Änderungen machen kann?

Vielleicht möchte man später mal bei einem String, der keine Zahl ist,
eine Exception werfen!?!

Damit sprichst du den größten Schwachpunk von atoi an. Es gibt dir keine
Auskunft darüber, ob die Konvertierung erfolgreich war.

int toInt(const std::string& s)
{
int retval;
std::stringstream stream(s);
if (! (stream >> retval))
throw SomeException();
return retval;
}

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Kurt Watzka
Guest





PostPosted: Sun May 02, 2004 4:33 am    Post subject: Re: string in Zahl umwandeln Reply with quote

Rolf Magnus wrote:

Quote:
Tobias Wollgam wrote:

Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int die
Funktion atoi oder gibt es was besseres?

std::ostringstream.

Ich glaube Du meinst std::istringstream (Er will String in Zahl, nicht
Zahl in String)


Kurt Watzka

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Rolf Magnus
Guest





PostPosted: Sun May 02, 2004 10:47 am    Post subject: Re: string in Zahl umwandeln Reply with quote

Kurt Watzka wrote:

Quote:
Rolf Magnus wrote:

Tobias Wollgam wrote:

Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int
die Funktion atoi oder gibt es was besseres?

std::ostringstream.

Ich glaube Du meinst std::istringstream (Er will String in Zahl,
nicht Zahl in String)

Richtig. Ich hab da falschrum gedacht. Wie man in meinem Beispiel dann
auch sieht, hätte ich eh eher einen stringstream genommen. Ich verstehe
sowieso den Sinn hinter dieser i/o/nix-stream-Klassenhierarchie.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Helmut Zeisel
Guest





PostPosted: Mon May 03, 2004 6:48 am    Post subject: Re: string in Zahl umwandeln Reply with quote

Tobias Wollgam wrote:

Quote:
Hallo.

Verwendet man zum Umwandeln von std::string in eine Zahl z.B. int die
Funktion atoi oder gibt es was besseres?

boost::lexical_cast

http://www.boost.org/libs/conversion/lexical_cast.htm

Helmut

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Stefan Reuther
Guest





PostPosted: Mon May 03, 2004 7:21 pm    Post subject: Re: string in Zahl umwandeln Reply with quote

Hallo,

Rolf Magnus wrote:
Quote:
Tobias Wollgam wrote:
[atoi]
Vielleicht möchte man später mal bei einem String, der keine Zahl ist,
eine Exception werfen!?!

Damit sprichst du den größten Schwachpunk von atoi an. Es gibt dir keine
Auskunft darüber, ob die Konvertierung erfolgreich war.

int toInt(const std::string& s)
{
int retval;
std::stringstream stream(s);
if (! (stream >> retval))
throw SomeException();
return retval;
}

Diese Implementation sagt dir aber auch nicht Bescheid, wenn die Eingabe
z.B. '12abc' war. Dazu müsste man das beispielsweise so ergänzen:
int toInt(const std::string& s)
{
int retval;
char c;
std::stringstream stream(s);
if (! (stream >> retval))
throw SomeException();
if (stream >> c)
throw SomeException();
return retval;
}
So richtig schön finde ich das aber nicht. Ich habe mir bisher immer
mein eigenes atoi geschrieben (ist ja nun nicht wirklich schwer).


Stefan

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.