C++23 Library - <spanstream> Header - GeeksforGeeks
source link: https://www.geeksforgeeks.org/cpp-23-library-spanstream-header/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C++23 Library – <spanstream> Header
The <spanstream> header is a new addition to C++ 23 Standard Libraries Collection. It provides fixed character buffer streams for input and output.
It is a collection of classes and function templates that let you manipulate letter stretches as if they were streams, much like <stringstream> or <istringstream>. However, it works with std::span<char> rather than pulling from or writing to a string or buffer.
The following image illustrates the inheritance diagram of the <spanstream> header file:
Inheritance diagram of basic_spanstream
Syntax to include <spanstream>
#include <spanstream>
The above statement imports all the function and class templates of the <spanstream> header in the std namespace of our program.
Class Templates in <spanstream>
Following is the list of classes defined inside the <spanstream> header file:
- basic_spanbuf
- basic_ispanstream
- basic_ospanstream
- basic_spanstream
- spanbuf
- wspanbuf
- ispanstream
- wispanstream
- spanstream
- wspanstream
All these classes have their own member functions and work.
std::spanstream
The std:;spanstream is a typedef name defined for basic_spanstream<char> that is nothing but a span-based string I/O buffer. We can initialize the objects of this class using std::span objects and then we can use it like a separate string buffer for input and output.
Example of <spanstream>
The following C Program demonstrates the use of spanstream class:
// C Program to demonstrate the use of spanstream #include <iostream> #include <spanstream> int main() { // A string with some data std::string data = "3.45 5.67 4.64" ; // A span<char> from the string std::span< char > inSpan(data); // creating a spanstream object initialized with spn std::spanstream inSpanStream(inSpan); // Use the >> operator to read three double values from // the spanstream double geeks_double1, geeks_double2, geeks_double3; inSpanStream >> geeks_double1 >> geeks_double2 >> geeks_double3; // Print out the value of double1, double2 and double3 std::cout << "geeks_double1 = " << geeks_double1 << "\n" ; std::cout << "geeks_double2 = " << geeks_double2 << "\n" ; std::cout << "geeks_double3 = " << geeks_double3 << "\n" ; // Another span containter with test string data std::string data_t = "This is a test" ; std::span< char > outSpan(data_t); // A spanstream from the span<char> std::spanstream outSpanstream(outSpan); // Use the << operator to write data to the spanstream // just like normal stream outSpanstream << "GeeksforGeeks " ; // Print out the data stored in outSpanstream std::cout << "Result: " << outSpanstream.rdbuf() << "\n" ; return 0; } |
Output
geeks_double1 = 3.45 geeks_double2 = 5.67 geeks_double3 = 4.64 Result: GeeksforGeeks
In the above example, we have used spanstream case to perform both input and output using >> and << operators.
Note: The above code can only be run on the compilers with C++ 23 standard support such as GCC 12 and MSVC 19.31 and onwards.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK