This is an old revision of the document!
Εκτός από τις κλάσεις για ανάγνωση ή εγγραφή από αρχείο ή τα stdin και stdout η standard βιβλιοθήκη της C++ παρέχει κλάσεις για ανάγνωση και εγγραφή από ένα string. Οι βασικές κλάσεις είναι οι εξής (παρέχονται αντίστοιχες κλάσεις και για wstrings, με τα οποία δεν θα ασχοληθούμε):
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream sstream; sstream << "Introducing C++ in CE325"; string token; while( sstream >> token ) { cout << token << " "; } cout << endl; }
Μπορούμε να χρησιμοποιήσουμε ένα stringstream προκειμένου να διαβάσουμε αριθμούς από ένα string ή να γράψουμε αριθμούσε σε ένα string, όπως παρακάτω:
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { string expr; cout << "Enter arithmetic expression: "; cin >> expr; istringstream stream(expr); }