VOOZH about

URL: https://www.geeksforgeeks.org/cpp/cpp-23-library-spanstream-header/

⇱ C++23 Library - <spanstream> Header - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++23 Library - <spanstream> Header

Last Updated : 6 May, 2023

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 stretch 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 <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:

  1. basic_spanbuf
  2. basic_ispanstream
  3. basic_ospanstream
  4. basic_spanstream
  5. spanbuf
  6. wspanbuf
  7. ispanstream
  8. wispanstream
  9. spanstream
  10. 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:


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.

Comment
Article Tags:
Article Tags: