Академический Документы
Профессиональный Документы
Культура Документы
* File: 20160916_CPP_L06.cpp.
* Title: 2016-09-16 C++ Lesson 06
* Author: Renato Montes
* Description: Notes for Albert Wei's C++ class
*/
#include <iostream>
using namespace std;
int main() {
cout << cin.rdbuf();
}
//obviously this only copies from cin into cout
}
//notice it's defined in ios, not ios_base
if(os.seekp(ios::off_type(-5)), ios_base::cur) {
// try to move back 5 bytes
...
}
//For stringstreams, the "get" & "put" positions are independent.
#include <iostream>
#include <sstream>
#include <string>
void read_int(istream& is) {
using namespace std;
int n;
if (is >> n)
cout << n << endl;
else
cout << "can't read an int" << endl;
}
int main() {
stringstream ss("12hello world"); //2 spaces inside string hello world
read_int(ss);
read_int(ss);
read_word(ss);
ss.clear();
read_word(ss);
ss << "goodbye!";
read_word(ss);
if (!ss.seekg(ios::off_type(-3), ios_base::cur))
cout << "can't seek" << endl;
read_word(ss);
read_word(ss);
}
/** pay attention to the state of the strings at various points */
//everything fails when string is not in a good state
/* Inheritance */
ios_base
/\
|
basic_ios<>
/\ /\
___________| \______________
basic_istream<> basic_ostream<>
/\ /\ /\ /\ /\ /\
| | |____ ________| | \
| | | | | \
| | basic_iostream<> | \
| | /\ /\ | \
| | | \ | \
basic_ifstream<> | | \ | \
ifstream | | \ basic_ofstream<> |
| | \ |
basic_istringstream<> | \ |
istringstream | \ |
basic_fstream<> | basic_ostringstream<>
fstream |
|
basic_stringstream<>
/* Default arguments */
//we can specify default values for certain parameters when
// _declaring_ a function