This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cpp:stringstreams [2019/05/20 11:38] gthanos |
cpp:stringstreams [2019/05/21 19:49] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== String streams ====== | ||
| - | |||
| - | Εκτός από τις κλάσεις για ανάγνωση ή εγγραφή από αρχείο ή τα //stdin// και //stdout// η // | ||
| - | |||
| - | * **istringstream: | ||
| - | * **ostringstream: | ||
| - | * **stringstream: | ||
| - | |||
| - | <code cpp stringstream.cpp> | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | using namespace std; | ||
| - | int main() { | ||
| - | stringstream sstream; | ||
| - | | ||
| - | sstream << " | ||
| - | string token; | ||
| - | while( sstream >> token ) { | ||
| - | cout << token << " "; | ||
| - | } | ||
| - | cout << endl; | ||
| - | | ||
| - | cout << " | ||
| - | cout << " | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== Μετατροπή αριθμών σε strings και αντίστροφα ===== | ||
| - | |||
| - | Μπορούμε να χρησιμοποιήσουμε ένα // | ||
| - | |||
| - | <code cpp stringstream.cpp> | ||
| - | #include < | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | using namespace std; | ||
| - | |||
| - | int main() { | ||
| - | char cexpr[256]; | ||
| - | double opnd1, opnd2; | ||
| - | char op; | ||
| - | cout << "Enter arithmetic expression: "; | ||
| - | cin.getline(cexpr, | ||
| - | istringstream sstream(cexpr); | ||
| - | sstream >> opnd1 >> op >> opnd2; | ||
| - | double result=0; | ||
| - | switch(op) { | ||
| - | case ' | ||
| - | result = opnd1 + opnd2; | ||
| - | break; | ||
| - | case ' | ||
| - | result = opnd1 - opnd2; | ||
| - | break; | ||
| - | case ' | ||
| - | result = opnd1 * opnd2; | ||
| - | break; | ||
| - | case '/': | ||
| - | result = opnd1 / opnd2; | ||
| - | break; | ||
| - | default: | ||
| - | cout << " | ||
| - | break; | ||
| - | } | ||
| - | cout << result << endl; | ||
| - | } | ||
| - | </ | ||
| - | |||