resources
Python to c++
Course objectives
- Understand the features of C++ and why they exist
- C++ documentation is no longer difficult to read
- Familiar with modern C++ design philosophy
Streams I
The Streams library is actually one of the older libraries in C++, but it’s worth starting with Streams because when you start learning C++, your first project will likely be a HelloWorld program, and you’ll probably want to use input and output Streams. This lesson will cover some of the flow details
The main content
- Overview + stringstream
- state bits
- Input/output streams
Reasons for using streams
One of the reasons we use streams is that we want our programs to interact with external devices: when we write programs, not all of the information we want to use is self-contained within the program code. Here are some of the peripherals we often use:
Console, keyboard and file are not described here. < span style = “box-sizing: border-box! Important; word-wrap: break-word! Important;”
Sockets are used in networks
For example, if we want to read floating point 3.14 in a file, we need to read the string representation of the floating point information: “3.14” and convert it to floating point 3.14
There are also string to object/structure conversions:
The key here is that you need to convert variables and data structures in your program to strings before you can transfer them to the console /socket.
There are two difficulties:
-
How do I read/write data as a string from an external source
-
How do we convert string data to the type we want
What stream does is encapsulate the first problem. When you use a stream, you don’t have to think about how the Stream library actually reads or writes data from an external device. You can think of a stream as a character buffer
When you use cout as an output stream, you don’t need to think about how cout interacts with Console.
The types of flow
Each stream has an input stream and an output stream:
Output stream: Insert data stream into buffer <<
Input stream: Extract data stream from buffer >>
stringstream
A string stream is a stream that is not linked to any external device
This is a good thing, because we can now just focus on how type conversions are done.
ostringstream
Ostringstream: output stringstream, where you write information to the output stream/you can send data to the output stream. When we build an OStringstream, we can give it a string to initialize.
#include <sstream> // stringstream
#include <iostream> // cin cout
using namespace std;
int main(a){
ostringstream oss("Ito En Green Tea");
cout << oss.str() << endl;
}
Copy the code
The common method is STR (), which takes the entire array in OSS and converts it to a string
[Compile instruction]
g++ -std=c++17 -Wall ostringstream.cpp -o oss
Copy the code
-wall opens warnings (Warn all, but not really all, you can add -wextra, etc.), and -o gives the executable a name. We can also write to OStringstream, that’s why it’s called an Output stringstream.
ostringstream oss("Ito En Green Tea");
cout << oss.str() << endl;
oss << 16.9 << " Ounce";
cout << oss.str() << endl;
Copy the code
Think about what kind of information will be printed?
Why is the output like this? The reason is that when you create an OSS object and open a stream, the pointer to the stream points to the starting position, and then when you write data to OSS, the pointer moves from the starting position and it overwrites the previously initialized content. (Note that “Ounce” is not an object of the String class, it is actually a C string.)
If you want to specify the offset of the pointer instead of the starting position when opening a stream, you can add additional arguments to the constructor when creating the stream:
ostringstream oss("Ito En Green Tea",ostringstream::ate)
: ate refers toat end
, the stream will point to the end when opened, and the output is:
Note: Another common parameter is:stringstream::binary
, indicating that the string stream is in binary mode.
There is only half of the video in the first section, and I supplemented the content in the rest of the PPT according to C++ Primer
istringstream
ostringstream oss("Ito En Green Tea");
oss << 16.9 << " Ounce";
istringstream iss(oss.str());
string output;
iss >> output;
cout << output << endl;
Copy the code
The above code only prints 16.9. If the next character in the input stream is a whitespace character, the input stream stops reading and continues reading skipping all leading whitespace.
Manually adjust the position of the flow
Manual flow position adjustment involves three interfaces/classes:
- To get the current stream position:
oss.tellp
andiss.tellg
Note that the output stream and the input stream have different methods - Create stream position offset:
streamoff(n)
- Set stream position:
oss.seekp(pos)
andiss.seekg(pos)
Example:
ostringstream oss("Ito En Green Tea");
oss << 16.9;
streampos pos = oss.tellp() + streamoff(3);
oss.seekp(pos);
oss << "Black";
cout << oss.str() << endl;
Copy the code
Output:
Another way of writing:
ostringstream oss("Ito En Green Tea");
oss << 16.9;
oss.seekp(streamoff(3),stringstream::cur);
oss << "Black";
cout << oss.str() << endl;
Copy the code
Stringstream ::cur indicates the current stream position.
Summary of common stringstream methods
- A constructor that takes an initial string:
istringstream iss("Initial");
.ostringstream oss("Initial");
Constructor runs add “modes” to set the stream position (ATE) and binary mode (corresponding to text mode) - The operator that inserts/fetches data into the stream buffer
oss << var
andiss >> var
, note that operators convert variable types to and from string types - Methods for adjusting the position of flow:
- Get location:
oss.tellp()
.iss.tellg()
- Setting position:
oss.seekp(pos)
andiss.seekg(pos)
- Create offset:
streamoff(n)
Often used to offset from the current position
- Get location: