Zahlenwerte aus einem String "filtern"
-
Badestrand schrieb:
edit: Nur aus Interesse, wie kann man das elegant mit der STL machen

Vielleicht so:
void ExtractCoords( string text, map<char,double>& coords ) { char last_char = 0; string::iterator pos = text.end(), last_pos = text.end(); while ( (pos = find_if(text.begin(),text.end(),isalpha)) != text.end() ) { if( pos == text.begin() ) { last_char = *pos; last_pos = pos; *pos = 0; continue; } stringstream sstr(""); copy(last_pos+1,pos,ostream_iterator<char>(sstr)); //da es blöderweise kein string::substr für iteratorn gibt double val = 0; sstr >> val; coords[last_char] = val; last_char = *pos; last_pos = pos; *pos = 0; } }Edit: gcc aus C::B will die find_if Anweisung nicht kompilieren

-
KasF schrieb:
copy(last_pos+1,pos,ostream_iterator<char>(sstr)); //da es blöderweise kein string::substr für iteratorn gibt
Meinst Du folgendes?string newstring(last_pos + 1, pos);
-
KasF schrieb:
Edit: gcc aus C::B will die find_if Anweisung nicht kompilieren

Das liegt daran dass isalpha dort wohl ein Makro ist (und imho auch sein darf). Zusätzlich könnten Dir die Überladungen aus <locale> einen Strich durch die Rechnung machen (betrifft alle Compiler).
-
LordJaxom schrieb:
KasF schrieb:
Edit: gcc aus C::B will die find_if Anweisung nicht kompilieren

Das liegt daran dass isalpha dort wohl ein Makro ist (und imho auch sein darf).
AFAIK ist 'isalpha' sowieso nur mit 'int'-Parametern definiert, d.h. die Signatur passt hier eh nicht.
-
Konrad Rudolph schrieb:
Meinst Du folgendes?string newstring(last_pos + 1, pos);

Wollte eigentlich sstr nen substring übergeben, aber habe voll vergessen, dass man einen string auch so konstruieren kann.LordJaxom schrieb:
Das liegt daran dass isalpha dort wohl ein Makro ist (und imho auch sein darf). Zusätzlich könnten Dir die Überladungen aus <locale> einen Strich durch die Rechnung machen (betrifft alle Compiler).
Achso, danke.
bool isAlpha(char c) { return isalpha(c); } ... find_if(text.begin(),text.end(),isAlpha) ... stringstream sstr( string(last_pos + 1,pos) );
-
Du musst aber, glaube ich, noch ne Kleinigkeit ändern, 'G' wird noch nicht erfasst (bei mir jedenfalls nicht). Das Problem hatte ich auch erst :p
^Und ich hab meinen Code nochmal verändert, 'text' kann ich ja verändern wie ich will :)^
-
Badestrand schrieb:
Du musst aber, glaube ich, noch ne Kleinigkeit ändern, 'G' wird noch nicht erfasst (bei mir jedenfalls nicht). Das Problem hatte ich auch erst :p
bool isAlpha(char c) { return isalpha(c); } void ExtractCoords( string text, map<char,double>& coords ) { char last_char = 0; string::iterator pos = text.end(), last_pos = text.end(); while ( (pos = find_if(text.begin(),text.end(),isAlpha)) != text.end() ) { if( pos == text.begin() ) { last_char = *pos; last_pos = pos; *pos = 0; continue; } stringstream sstr( string(last_pos + 1,pos) ); double val = 0; sstr >> val; coords[last_char] = val; last_char = *pos; last_pos = pos; *pos = 0; } stringstream sstr( string(last_pos+1,text.end()) ); double val = 0; sstr >> val; coords[last_char] = val; }
Das kann man aber bestimmt noch eleganter lösen, vielleicht zaubert ja Konrad ne rekursive Lösung ...
-
Und nochmal was gefrickelt:
bool isAlpha(char c) { return isalpha(c); } template<class T, class S> T lexical_cast(const S& val) { stringstream sstr(""); T newval; if( !( sstr << val && sstr >> newval ) ) throw bad_cast(); return newval; } void ExtractCoords( string text, map<char,double>& coords ) { typedef string::iterator sIT; struct helpFunc { void operator()(char& last_char, sIT& pos, sIT& last_pos) { last_char = *pos; last_pos = pos; *pos = 0; } }; char last_char = 0; sIT pos = text.end(), last_pos = text.end(); while ( (pos = find_if(text.begin(),text.end(),isAlpha)) != text.end() ) { if( pos == text.begin() ) { helpFunc()(last_char,pos,last_pos); continue; } coords[last_char] = lexical_cast<double>( string(last_pos + 1,pos) ); helpFunc()(last_char,pos,last_pos); } coords[last_char] = coords[last_char] = lexical_cast<double>( string(last_pos+1,text.end()) ); }
-
hehe :p:
void ExtractCoords( string text, map<char,double>& coords ) { for ( int i=text.length()-1; i>=0; --i ) { if ( isalpha( text[i] ) ) { coords[ text[i] ] = atof( text.c_str()+i+1 ); text[i] = '\0'; } } }
-
Noch ein Nachtschwärmer

Elegante Idee von Rückwärts durchzulaufen. Wenn die Sonne aufgeht, schreibe ich meins um ...
-
geSTL'te Version:
void ExtractCoords( string text, map<char,double>& coords ) { string::reverse_iterator pos = text.rend(); while ( ( pos = find_if(text.rbegin(), text.rend(), isAlpha) ) != text.rend() ) { size_t dis = distance(pos,text.rend()); coords[*pos] = lexical_cast<double>( text.substr(dis) ); text.erase(dis-1); } }