Lecture 4: Streams...You can return references // Note that the parameter must be a non-const...
Transcript of Lecture 4: Streams...You can return references // Note that the parameter must be a non-const...
![Page 1: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/1.jpg)
Lecture 4: StreamsCS 106L, Winter ‘21
![Page 2: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/2.jpg)
Today’s Agenda
● Recap: References● Streams● File Streams● String Streams● Buffering● State Bits● Chaining
![Page 3: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/3.jpg)
Recap: Something
![Page 4: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/4.jpg)
Uniform initialization
int x{3}; // One syntax to initialize all variables
// Both of these are vectors with elements {3, 5, 7}std::vector<int> a_vector{3, 5, 7};std::vector<int> another_vector = {3, 5, 7};
Student s = {"Ethan", "CA", 20};
![Page 5: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/5.jpg)
References
int b = 5;int& a = b;a = 2; // now b = 2 as well
void switch(int& c) { c = 3; }switch(a); // now b = 3 as well
const int& d = a;d = 5; // fails because const reference
![Page 6: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/6.jpg)
You can return references
// Note that the parameter must be a non-const reference to return// a non-const reference to one of its elements!int& front(std::vector<int>& vec) { // assuming vec.size() > 0 return vec[0];}
int main() { std::vector<int> numbers{1, 2, 3}; front(numbers) = 4; // vec = {4, 2, 3} return 0;}
Common idiom: return an element inside our class.
![Page 7: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/7.jpg)
Streams
![Page 8: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/8.jpg)
A stream is an abstraction for input/output
![Page 9: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/9.jpg)
<< “Hello World”
std::cout
![Page 10: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/10.jpg)
“Hello World”
std::cout
![Page 11: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/11.jpg)
“Hello World”
std::cout
![Page 12: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/12.jpg)
cout << “Strings work!” << endl;cout << 1729 << endl;cout << 3.14 << endl;cout << “Mixed types: ” << 1123 << endl;
Streams can take different types of input!
Any primitive type can be inserted; for other types, you need to explicitly tell C++ how to do this
![Page 13: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/13.jpg)
Streams convert between the string representation of data and the data itself.
![Page 14: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/14.jpg)
Idea: both input and output are strings; need to do computation on object representation
![Page 15: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/15.jpg)
Types of Streams
![Page 16: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/16.jpg)
Output Streams
std::cout << 5 << std::endl; // prints 5 to the console
std::ofstream out(“out.txt”, std::ofstream::out);out << 5 << std::endl; // out.txt contains 5
● Of type std::ostream● Can only receive data with the << operator
○ Converts data to string and sends it to stream
![Page 17: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/17.jpg)
Live Code Demo:Ostreams.cpp
![Page 18: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/18.jpg)
int x;std::cin >> x;
// what happens if input is 5 ?// how about 51375 ?
Input Streams
What does this do?
![Page 19: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/19.jpg)
Input Streams
// input: 5 abc 7int x, z; string y;std::cin >> x > y > z; // x=5, y=“xyz”, z=7
// std::ifstream does the same for filesstd::ifstream in(“out.txt”, std::ifstream::in);in >> x >> y >> z; // out.txt contains 5
● Of type std::istream● Can only give you data with the >> operator
○ Receives string from stream and converts it to data
![Page 20: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/20.jpg)
🤔 Questions? 🤔
![Page 21: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/21.jpg)
Why does this work?
![Page 22: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/22.jpg)
Think of a std::istream as a sequence of characters
4 2 a b 4 \n
position
int x; string y; int z;input >> x; input >> y;input >> z;
![Page 23: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/23.jpg)
Extracting an int reads as many characters as possible until whitespace
4 2 a b 4 \n
position
int x; string y; int z;input >> x; // 42 put into xinput >> y;input >> z;
![Page 24: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/24.jpg)
Next time, first skip over any whitespace
4 2 a b 4 \n
position
int x; string y; int z;input >> x; input >> y; // ab put into yinput >> z;
![Page 25: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/25.jpg)
When no more data is left, fail bit set to true
4 2 a b 4 \n
position
int x; string y; int z;input >> x; input >> y;input >> z; // 4 put into z
![Page 26: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/26.jpg)
int x;std::cin >> x;std::cout << x * 5 << std::endl;
// what happens if input is blah ?
Input Streams
Given what we just learned, what does this do? Answer in the chat.
![Page 27: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/27.jpg)
Reading using >> extracts a single “word”including for strings
To read a whole line, use getline(istream& stream, string& line);
![Page 28: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/28.jpg)
Don’t mix >> with getline!
● >> reads up to the next whitespace character and does not go past that whitespace character.
● getline reads up to the next delimiter (by default, ‘\n’), and does go past that delimiter.
● Don’t mix the two or bad things will happen!
📝 Note for 106B/X: Don’t use >> with Stanford libraries, which use getline.
![Page 29: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/29.jpg)
input.get(ch); // reads a single charinput.clear(); // resets the fail bitinput.open(“filename”); // opens stream on a fileinput.seekg(0); // rewinds stream to start
input.close(); // closes stream// done automatically for you,// so not necessary
Additional Stream Methods
![Page 30: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/30.jpg)
std::iostream is both a istream & ostream
![Page 31: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/31.jpg)
🤔 Questions? 🤔
![Page 32: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/32.jpg)
Stringstreams
![Page 33: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/33.jpg)
std::string input = “5 seventy 2”;
std::istringstream i(input);int x; std::string y; int z;i >> x >> y >> z;
std::cout << z << std::endl;
Work with a string as if it were a stream
![Page 34: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/34.jpg)
Live Code Demo:String Streams
![Page 35: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/35.jpg)
Stream Internals
![Page 36: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/36.jpg)
Stream Internals
● Buffering
● State Bits
● Chaining (a.k.a. why << << << works)
![Page 37: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/37.jpg)
Buffering
![Page 38: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/38.jpg)
Writing to console/file is slow.If we had to write each character separately, slow runtime.
![Page 39: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/39.jpg)
Accumulate characters in a temporary buffer.
h e l l o w o r l d
input << “hel”;input << “lo ”;input << “world”;
When full, write entire buffer to output
Output: hello world
![Page 40: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/40.jpg)
stream << std::flush; // flush what we have so farstream << std::endl; // flush with newline// this is equivalent to: stream << “\n” << std::flush;
Empty the buffer early by flushing:
![Page 41: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/41.jpg)
Buffer Takeaways
● The internal sequence of data stored in a stream is called a buffer.● Istreams use buffers to store data we haven’t used yet.● Ostreams use buffers to store data that hasn’t been outputted yet.
🤔 There’s actually a third standard stream, std::cerr, which is not buffered. Why?
![Page 42: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/42.jpg)
State Bits
![Page 43: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/43.jpg)
Streams have four state bits
G Good bit: whether ready for read/write
F Fail bit: previous operation failed, future operations frozen
E EOF bit: previous operation reached end of file
B Bad bit: external integrity error
![Page 44: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/44.jpg)
// here’s a very common read loop:
while (true) { stream >> temp; // read data if (stream.fail()) break; // checks for fail bit OR bad bit doSomething(temp);}
Using State Bits
![Page 45: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/45.jpg)
Streams can be converted to bool
stream >> temp;if (stream.fail()) break; // checks for fail bit OR bad bitdoSomething(temp);
stream >> temp;if (!stream) break; // same thingdoSomething(temp);
![Page 46: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/46.jpg)
Aside: Chaining
![Page 47: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/47.jpg)
Chaining >> and <<
>> and << are actually functions!
operator<<(std::cout, “hello”);
std::cout << “hello”;
operator>>(std::cout, temp);
std::cin << temp;
std::ostream& operator<<(std::ostream& out, const std::string& s);
std::ostream& operator<<(std::ostream& out, const std::string& s);
![Page 48: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/48.jpg)
This is how the magic std::cout mixing types works!
cout << “test” << 5;
operator<<(operator<<(cout, “test”), 5);
// (cout << “test”) << 5;
operator<<(cout, 5);
cout
std::ostream& operator<<(std::ostream& out, const std::string& s);std::ostream& operator<<(std::ostream& out, const int& i);
![Page 49: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/49.jpg)
// here’s a very common read loop:
while (true) { stream >> temp; // read data if (!stream) break; // checks for fail bit OR bad bit doSomething(temp);}
Using State Bits — Part 2
This returns the stream itself!
![Page 50: Lecture 4: Streams...You can return references // Note that the parameter must be a non-const reference to return // a non-const reference to one of its elements! int& front(std::vector&](https://reader036.fdocuments.in/reader036/viewer/2022071302/60b07f6ec86c616f755c73f8/html5/thumbnails/50.jpg)
// here’s a very common read loop:
while (stream >> temp) { doSomething(temp);}
Using State Bits — Part 2